This is for c programming!! I have opened a txt file with fopen and saved it to
ID: 639074 • Letter: T
Question
This is for c programming!!
I have opened a txt file with fopen and saved it to fp now I have a function called getFollowingWord(FILE *fp); How would i go through the file and print out the strings on each line removing whitespace i am going through the file by getting each character of the string so please do the same.
For example text file contains
Tree
Blue cheese
The output should be
Tree
blue
cheese
Max size of the word can be 200 You can use isspace function if you like
Example of code
for(int i = 0; i < Max_Size; i++){
word[i] = fgetc(fp);
-----------------what do i do here to check for whitespace and remove it and place words followed by whitespace
on there own line.
}
Explanation / Answer
#include <stdio.h>
void getFollowingWord(FILE *fp){
char c;
char oneword[200];
int flag = 0;
while(1) {
c = fgetc(fp);
if(c == EOF){
break;
}
if(isspace(c)){
flag = 1;
}
else{
if(flag == 1){
printf(" ");
flag = 0;
}
printf("%c", c);
}
}
}
int main(){
FILE *fp;
fp = fopen("file1.txt", "r");
getFollowingWord(fp);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.