So I have a text file which reads: This line has numbers: 1, 2,3, 4, 55 Some lines include punctuations such as , ; . / & ^
My output is displaying drastic results:
Number of vowels: 0
Number of consonents: 242
Number of digits: 66
Number of white spaces: 32
Number of other characters: 55
Why is it doing this if the code is just reading til the end of the file?
Here's my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void output_summary (int space_count, int ch_count, int digit_count, int vowel_count, int sym_count);
int main (void)
{
char ch;
int status;
int space_count = 0, ch_count = 0, digit_count = 0, vowel_count = 0, sym_count = 0;
FILE *infile;
infile=fopen("input.txt", "r");
if (infile == NULL)
{
printf("ERROR COMPUTING FILE\n");
exit(EXIT_FAILURE);
}
status = fscanf(infile, "%c", &ch);
while (status != EOF)
{
if (isspace(ch))
{
space_count++;
}
else if (isupper(ch))
{
ch = tolower(ch);
ch_count++;
}
else if (islower(ch))
{
ch_count++;
}
else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
vowel_count++;
}
else if (isdigit(ch))
{
digit_count++;
}
else
{
sym_count++;
}
status = fscanf(infile, "%c", &ch);
}
fclose(infile);
output_summary (space_count, ch_count, digit_count, vowel_count, sym_count);
return (0);
}
void output_summary (int space_count, int ch_count, int digit_count, int vowel_count, int sym_count)
{
FILE *outfile;
outfile = fopen("output.txt", "w");
fprintf(outfile, "Number of vowels: %d\n", vowel_count);
fprintf(outfile, "Number of consonents: %d\n", ch_count);
fprintf(outfile, "Number of digits: %d\n", digit_count);
fprintf(outfile, "Number of white spaces: %d\n", space_count);
fprintf(outfile, "Number of other characters: %d\n", sym_count);
fclose(outfile);
}