Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

my (count.c program) is working. When the program prompt Enter file name: I enter jabberwocky.txt. This jabberwocky.txt is inside the folder of my code. When

my (count.c program) is working. When the program prompt "Enter file name:" I enter jabberwocky.txt. This jabberwocky.txt is inside the folder of my code. When I run the program, the program prints out like this " jabberwocky.txt has 1043 characters"

image text in transcribed

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!

Need to do:

1) Word identifies 977 characters including spaces. Your count.c believes there are 1043. Make the corrections necessary to your code to count only the visible characters and spaces!

image text in transcribed

2) Count and print out the total number of characters without spaces! (The ctype.h will come in handy.)

3) Figure out how to count the lines and then display the count at the end. Notice in Word, that a line return at the end of the line does not add a line to the lines count. Notice that a new, blank file does not count any lines. (HINT: Count the lines by looking for a line return and then use an if statement with compound conditions at the end to decide if you should count one more line. With the approach, you will need to store the previous value of ch on each loop iteration.)

4) Figure out how to count words. Words are delineated by spaces or new lines. (We will not worry about hyphenated words at the end of a line!) You will need to set up a 'flag'. Flags often contain T/F and using 1/0 for values works perfectly. Use a flag to mark whether or not you have started looking at a word. Change the flag to 0 once a space or new line is encountered and increase the word count.

Here is some pseudocode...

declare begWord and initialize to 0 if ch is not a space, then set begWord to 1 if begWord is 1 AND the current ch is a white space character.... increase the word count reset begWord to 0

#include #include // ANSI C exit () prototype #include // strchr() prototype #define SLEN 81 char * s_gets (char st, int n) int main void) int ch; FILE *fp; long count 0; char filename [SLEN] // place to store each character as read I/ "file pointer" printf("Enter file name:"); s_gets(filename, SLEN) if ((fp = fopen(filename, "r")) = NULL) printf( "can't open %s ", exit (EXIT_FAILURE); filename); while ((ch= getc(fp)) != E0F) putchar(ch) count++ fclose(fp); printf( "File %s return 0; %ld characters ", filename, count); char * s_gets (char st, int n) char * ret_val; char * find; ret_val = fgets(st, n, stdin ) ; if (ret_val) . .); find = strchr(st, if (find) // look for newline // if the address is not NULL // place a null character there *find '10' else (getchar() continue; while != . .) return ret_val; #include #include // ANSI C exit () prototype #include // strchr() prototype #define SLEN 81 char * s_gets (char st, int n) int main void) int ch; FILE *fp; long count 0; char filename [SLEN] // place to store each character as read I/ "file pointer" printf("Enter file name:"); s_gets(filename, SLEN) if ((fp = fopen(filename, "r")) = NULL) printf( "can't open %s ", exit (EXIT_FAILURE); filename); while ((ch= getc(fp)) != E0F) putchar(ch) count++ fclose(fp); printf( "File %s return 0; %ld characters ", filename, count); char * s_gets (char st, int n) char * ret_val; char * find; ret_val = fgets(st, n, stdin ) ; if (ret_val) . .); find = strchr(st, if (find) // look for newline // if the address is not NULL // place a null character there *find '10' else (getchar() continue; while != . .) return ret_val

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions