Question: This is C language assignment. The following program is working; however, I want you to code differently. Please remember that the output has to be
This is C language assignment. The following program is working; however, I want you to code differently. Please remember that the output has to be like this:
Words 166
Visible Characters 977
Characters without spaces 762
Lines 34
When you run the program: enter jabberwocky.txt
Below is
jabberwocky.txt
'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.
#include
#include // exit() prototype
#include
#define SLEN 81 /* from reverse.c */
#define SPACE ' '
void countall(void)
{
int ch, prev; // place to store each character as read
FILE *fp; // "file pointer"
unsigned long count = 0;
unsigned long countVisi = 0;
unsigned long noSpaces = 0;
unsigned long lineCount = 0;
unsigned long wordCount = 0;
int begWord = 0;
char file[SLEN]; /* from reverse.c */
puts("Enter the name of the file to be processed:");
scanf("%80s", file);
if ((fp = fopen(file,"r")) == NULL) /* read mode */
{
printf("count program can't open %s ", file);
exit(EXIT_FAILURE);
}
while ((ch = getc(fp)) != EOF)
{
putc(ch,stdout); // same as putchar(ch);
count++;
if(ch != ' ' && ch != ' '){
countVisi++;
}
if(!isspace(ch) && ch != ' ' && ch != ' '){
begWord = 1;
noSpaces++;
}
if(ch == ' '){
lineCount++;
}
if(begWord == 1 && isspace(ch)){
wordCount++;
begWord = 0;
}
prev = ch;
}
fclose(fp);
if(prev != ' ' && prev != ' '){
lineCount++;
wordCount++;
}
printf(" File %s has %lu characters ", file, count);
printf("%7lu visible characters ", countVisi);
printf("%7lu characters without spaces ", noSpaces);
printf("%7lu lines ", lineCount);
printf("%7lu words ", wordCount);
}
Note: I call this program from the main driver. So, you might need to change it into int main instead of void countall void. One more time, the code has to be coded differently but the result has to be the same, OK?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
