Letter Counter


General Statement: Read a sentence and output the count of the number of 1 letter words, the number of 2-letter words, etc.

Input: The first line of the data set for this problem is an integer that represents the number of sentences that follow. Each sentence is on a separate line.

Output: Do not output any counts of 0. The letter counts are to be in ascending order by the number of letters in the word. All letters are upper case.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: Any punctuation in the sentence is not part of the word length. The maximum word length is 10 letters. The maximum sentence length is 70 characters.

Sample Input:
3
TODAY’S SATURDAY
HI SAM, HOW ARE YOU?
MARCH, THEN APRIL, THEN MAY

Sample Output:
COUNT OF 6 = 1
COUNT OF 8 = 1

COUNT OF 2 = 1
COUNT OF 3 = 4

COUNT OF 3 = 1
COUNT OF 4 = 2
COUNT OF 5 = 2

Solution :


#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main ()
{
    int cases;
    scanf ("%d", &cases);
    getchar ();

    while ( cases-- ) {
        char sentence [75];
        int frq [10];

        for ( int i = 0; i < 10; i++ )
            frq [i] = 0;

        gets (sentence);

        int length = strlen (sentence);

        int i = 0;
        while ( i <= length ) {
            int count = 0;

            while ( sentence [i] != ' ' && sentence [i] != '' ) {
                if ( isalpha (sentence [i]) )
                    count++;
                i++;
            }
            i++;
            frq [count - 1]++;
        }

        for ( int i = 0; i < 10; i++ ) {
            if ( frq [i] != 0 )
                printf ("COUNT OF %d = %d\n", i + 1, frq [i]);
        }

        printf ("\n");
    }

    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s