Saturday 11 November 2017

C Program to Count the Number of Unique Words


Code:

#include  
#include
#include
int main()
{
    int i = 0, e, j, d, k, space = 0;
    char a[50], b[15][20], c[15][20];

    printf("Read a string:\n");
    fflush(stdin);
    scanf("%[^\n]s", a);
    for (i = 0;a[i] != '\0';i++)        //loop to count no of words
    {
        if (a[i] =  = ' ')
            space++;
    }
    i = 0;
    for (j = 0;j<(space + 1);i++, j++)    //loop to store each word into an 2D array
    {
        k = 0;
        while (a[i] != '\0')
        {
            if (a[i] == ' ')
            {
                break;
            }
            else
            {
                b[j][k++] = a[i];
                i++;
            }
        }
        b[j][k] = '\0';
    }
    i = 0;
    strcpy(c[i], b[i]);
    for (e = 1;e <= j;e++)        //loop to check whether the string is already present in the 2D array or not
    {
        for (d = 0;d <= i;d++)
        {
            if (strcmp(c[i], b[e]) == 0)
                break;
            else
            {
                i++;
                strcpy(c[i], b[e]);
                break;
            }
        }
    }
    printf("\nNumber of unique words in %s are:%d", a, i);
    return 0;
}


Output:

Read a string:
Welcome to  C-programming class,  Welcome again to C class!
The length of input string is:58

Number of unique words in Welcome to C-programming class,  Welcome again to C class! are:7


More C Programs











100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...