Program to count the total number of words in a string.


C File =>


/* Write a program to count the total number of words in a string. */


#include<stdio.h>
#include<string.h>
int main()
{
    int i,wcnt=0;;
    char s[50];
    printf("\n Enter your String : ");
    scanf("%[^\n]s",s);
    printf("\n string = %s",s);
    for(i=0;s[i]!='\0';i++)
   {
         if(s[i]==' ' && s[i+1]!=' ')
            wcnt++;
    }
     printf("\n Word Count = %d",wcnt+1);
    return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.