Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming We would like to modify the program to tell us the size of the big

ID: 649544 • Letter: C

Question

C programming

We would like to modify the program to tell us the size of the biggest

and smallest words and to count four letter words (all words that have exactly 4 letters) in

text redirected to the input. The program should just print the number of four letter words

detected.

For example, the input:

How many four letter words can you spot in this text?

with this too.

would output:

There were 7 four letter words

The biggest word had 6 letters

The smallest word had 2 letters

Mark up the code on the next page to show the modications needed for this task.

#include <stdio.h>

#include "tfdef.h"

#include "chrutil.h"

main()

{ signed char ch;

int inword, /* flag indicating when in a word */

lns, wds, chrs; /* Counters for lines, words, chars. */

lns = wds = chrs = 0; /* initialize counters to 0 */

inword = FALSE; /* before beginning we are not in a word */

while ((ch = getchar()) != EOF) /* while there are more chars */

{ chrs = chrs + 1; /* count characters */

if (ch == ' ') /* if newline char */

lns = lns + 1; /* count lines */

/* if not in a word and not a delimiter */

/* then this must be the beginning of a word */

if (!inword && !delimitp(ch))/* if not in word and not delim*/

{ inword = TRUE; /* remember we are in a word */

wds = wds + 1; /* count words */

}

/* otherwise if in a word, but found a delimiter */

/* then we just went beyond the end of the word */

else if (inword && delimitp(ch))/* if in word and a delim*/

{ inword = FALSE; /* no longer in word */

putchar(' '); /* end word with newline */

}

if (inword) /* if in a word */

putchar(ch); /* print the character */

}

}

Explanation / Answer

/* count the letters in a word when inside the loop that counts the number of words, and compare with the biggest and smallest till that input ,also check if it was 4 letter word */

#include <stdio.h>

#include "tfdef.h"

#include "chrutil.h"

main()

{ signed char ch;

int inword, /* flag indicating when in a word */

biggest, smallest, let4wds, wordlen = 0;

lns, wds, chrs; /* Counters for lines, words, chars. */

lns = wds = chrs = 0; /* initialize counters to 0 */

inword = FALSE; /* before beginning we are not in a word */

while ((ch = getchar()) != EOF) /* while there are more chars */

{ chrs = chrs + 1; /* count characters */

if (ch == ' ') /* if newline char */

lns = lns + 1; /* count lines */

/* if not in a word and not a delimiter */

/* then this must be the beginning of a word */

if (!inword && !delimitp(ch))/* if not in word and not delim*/

{ inword = TRUE; /* remember we are in a word */

wds = wds + 1; /* count words */

wordlen = wordlen+1; /* count the letters in a word */

}

/* otherwise if in a word, but found a delimiter */

/* then we just went beyond the end of the word */

else if (inword && delimitp(ch))/* if in word and a delim*/

{ inword = FALSE; /* no longer in word */

if (wordlen > biggest) { biggest = wordlen;}

if(wordlen < smallest) { smallest = wordlen;}

if(wordlen == 4) {let4wds = let4wds + 1;}

putchar(' '); /* end word with newline */

}

if (inword) /* if in a word */

putchar(ch); /* print the character */

}

/* print the biggest, smallest and number of 4 letter words */

printf("The biggest word has %d letters", biggest);

printf("The smallest word has %d letters", smallest);

printf("The number of 4 letter words are %d", let4wds);

}