This program is supposed to be able to count the number of words in a text file.
ID: 3644622 • Letter: T
Question
This program is supposed to be able to count the number of words in a text file. I keep getting errors or am not able to run the program. I can't figure out what I am doing wrong. Any help would be appreciated.#include<stdio.h>
#include<ctype.h>
int main(void)
{
FILE *infile = fopen("out.txt","r");
FILE *outfile = fopen("wordlist.txt","w");
int count=0;
if ( infile != NULL && outfile != NULL )
{
int terminate = 0;
for ( ;; )
{
int ch = fgetc(infile);
if ( ch == EOF )
{
break;
}
if ( isalpha(ch) )
{
fputc(ch, outfile);
terminate = 1; /* okay to add newline on next space */
}
else if ( isspace(ch) && terminate )
{
count++;
fputc(' ', outfile);
terminate = 0;
}
}
printf("%d", count);
fclose(infile);
fclose(outfile);
}
return 0;
}
Explanation / Answer
f ( isalpha(ch) ) { fputc(ch, outfile); terminate = 1; /* okay to add newline on next space */ } else if ( isspace(ch) && terminate ) { count++; fputc(' ', outfile); terminate = 0; } ## else is missing; Note: After 'if else' used it should follow an 'else' statement according to the syntax. add an 'else' stmt & run it.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.