Program to count number of words from a given sentence

The below mentioned C Program will count number of words from a given sentence. The program will accept a sentence in a character array sen[100]. It will then calculate the number of words and display the result.

#include
#include
#include

void main()
{
    char sen[100];
    int i, count = 0;
    
    printf("Enter a Sentence : \n");
    gets(sen);
    
    for (i=0; i<strlen(sen);i++)
    {
        if (isspace(sen[i]))
        {
        	count++;
        }
    }
    
    printf("Number of words in the sentence = %d", ++count);
    printf("\n");
    
    getch();
}

Also See:
Strings in C Programming Language

You may also like...

11 Responses

  1. Anonymous says:

    Doesn’t work there is a syntax error stating that it is missing ‘;’ before ‘)on line 12.

  2. Anonymous says:

    for (i=0;iand count=1

  3. Anonymous says:

    u hve entr a erong entry… that is getch()

  4. Anonymous says:

    It work’s

  5. Anonymous says:

    what if there is ‘,'(comma) symbol in the given sentence??

  6. Anonymous says:

    this does not work for sentence having multiple spaces between two words

  7. Anonymous says:

    very good work , although there are some mistake .

    I correct it and here is the new code :

    #include
    #include

    using namespace std;

    int main()
    {
    char sen[100];
    int i, count = 0;

    printf(“Enter a Sentence : \n”);
    gets(sen);

    for (i=0; i {
    if (isspace(sen[i]))
    {
    count++;
    }
    }

    printf(“Number of words in the sentence = %d”, ++count);
    printf(“\n”);

    system(“pause”);

    }

  8. Reshma K C says:

    can anyone do the same code using pointers??

  9. Anonymous says:

    what if I give the input as –
    Hello I am there.

    ur ouput will be wrong..

  10. DEEPAK THOMAS says:

    what if there are 3 spaces before each word ???? each time it wil count the spaces as words ryt ???? how is it possible?

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet