I am trying to write a program in C that will read an undetermined amount of tex
ID: 3937348 • Letter: I
Question
I am trying to write a program in C that will read an undetermined amount of text (looking for EOF) and read 80 characters at a time then assigning each separate chunk of 80 characters to a pointer which then can be referenced by another pointer. This is what I came up with so far, but it doesn't make much sense to me.
(thought the picture would've uploaded so here's the text)
#define CHUNK = 81
int main(){
char buf[CHUNK];
char *ptr;
//char *mainPtr;
int i, j;
ptr = (char*)malloc(100 * sizeof(char));
printf("Enter text: ");
for(j = 0; j != EOF; j++) {
for(i = 0; i < 80; i++){
scanf("%[^ ]s", &buf[i]);
ptr = &buf[CHUNK];
}
ptr = &buf[CHUNK];
LengthOfWords(ptr); }
Explanation / Answer
//no need to allocate ptr, if it has only point to the read string, and j != EOF is incorrect
//read input from some text file. if you are unable to provide EOF character when adding in console
#include "stdio.h"
#define CHUNK 81
int main(){
char buf[CHUNK];
char *ptr;
int i, j;
printf("Enter text: ");
while( scanf("%80s",buf) != EOF ){
ptr = buf;
//now ptr refers to read string
//run function on ptr
printf("%s ", ptr);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.