Search code examples
ccharactercountingwords

Counting words separated by symbols as two words


#include <stdlib.h>
#include <stdio.h>

int main()
{
    unsigned long c;
    unsigned long line;
    unsigned long word;
    char ch;
    char lastch = -1;

    c = 0;
    line = 0;
    word = 0;

    while((ch = getchar()) != EOF)
    {
        c ++;
        if (ch == '\n')
        {
            line ++;
        }
        if (ch == ' ' || ch == '\n')
        {
            if (!(lastch == ' ' && ch == ' '))
            {
                word ++;
            }
        }
        lastch = ch;
    }
    printf( "%lu %lu %lu\n", c, word, line );
    return 0;
}

So this program counts the number of characters, lines or words in standard input. But one of the requirements is that the words separated by any of the symbols such as , !, -, +, etc.. must be considered 2 words. How would I modify my code to do that?


Solution

  • Create an array of characters which represent the separation of words. Modify the second if condition inside the while loop to check if ch is present in the array and lastch is not present in that array.

    Modified code:

    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    unsigned long c;
    unsigned long line;
    unsigned long word;
    char ch;
    char lastch = -1;
    int A[256] = {0};
    
    //Initialize the array indexes which are to be treated as separators.
    //Next check the presence of any of these characters in the file.
    
    A[' '] = 1; A['+'] = 1; A['!'] = 1; A['-'] = 1; A['\n'] = 1;
    c = 0;
    line = 0;
    word = 0;
    
    while((ch = getchar()) != EOF)
    {
        c ++;
        if (ch == '\n')
        {
            line ++;
        }
        if (A[ch] == 1)
        {
            if (!(A[lastch] == 1 && A[ch] == 1))
            {
                word ++;
            }
        }
        lastch = ch;
    }
    printf( "%lu %lu %lu\n", c, word, line );
    return 0;
    }