Monday, July 31, 2017

Count no. of words in a sentence in C

#include<stdio.h>
void main()
{
    char str[50],i,wordcount=0;
    printf("Enter a line of text : ");
    gets(str);
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]==32)
        {
           wordcount++;
        }
        if(str[i]==' ' && str[i-1]==' ')
        {
            wordcount--;
        }



    }
    if(str[0]==' ' || str[0]=='\0')
    {
        wordcount=-1;
    }


    printf("\n\nThe number of words in the line of text entered is %d",wordcount+1);
    getch();
}

0 comments:

Post a Comment