Question
This is c language. My code is almost perfect. You just need to figure out the missing parts that I could not figure it out.
This is c language. My code is almost perfect. You just need to figure out the missing parts that I could not figure it out.
When I run jabberwocky.txt: I got the following
Characters(with spaces) : 977
Characters(no spaces) : 762
Lines: 66
Words: 165
But the the output has to be:
Characters(with spaces) : 977
Characters(no spaces) : 762
Lines: 34
Words: 166
So, please help me figure out how to get (Lines: 34) and (Words: 166) instead of (Lines: 66) and (Words: 165).
here is the code:
#include
void countall(char *filename);
int main() { char filename[SLEN]; /* from reverse.c */
puts("Enter the name of the file to be processed:"); scanf("%80s", filename);
countall(filename); }
void countall(char *filename) { unsigned long lines = 0; unsigned long totalChars = 0; unsigned long words = 0; unsigned long nonSpaceChars = 0; unsigned long visibleChars = 0; unsigned long begWord = 0; FILE *fp;
char ch;
fp = fopen(filename, "r"); if(fp == NULL) { printf("ERROR: could not open input file %s ", filename); exit(1); }
while((ch = fgetc(fp) ) != EOF) { putchar(ch); totalChars++;
if(isspace(ch)) { if(ch == ' ' || ch == ' ') lines++; else visibleChars++;
if(begWord == 1) { begWord = 0; words++; } } else { nonSpaceChars++; visibleChars++; begWord = 1; } }
fclose(fp); printf(" ************************* "); printf(" File %s has %lu total characters: ", filename, totalChars); printf("\Characters(with spaces): %lu ", visibleChars); printf("\Characters(no spaces) : %lu ", nonSpaceChars); printf("\Lines: %lu ", lines); printf("\Words: %lu ", words);
}
Here is the jabberwocky.txt if you need to test out.
'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe.
"Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!"
He took his vorpal sword in hand: Long time the manxome foe he sought- So rested he by the Tumtum tree, And stood awhile in thought.
And, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came wiffling through the tulgey wood, And burbled as it came!
One, two! One, two! And through and through The vorpal blade went snicker-snack! He left it dead, and with its head He went galumphing back.
"And hast thou slain the Jabberwock? Come to my arms, my beamish boy! O frabjous day! Callooh! Callay!" He chortled in his joy.
'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started