Showing posts with label Write a C program to find number of words in a given sentence.. Show all posts
Showing posts with label Write a C program to find number of words in a given sentence.. Show all posts

Write a C program to find number of words in a given sentence.

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
    char str[45];
    int count=0,i;
    clrscr();
    printf("Enter the Sentence: ");
    gets(str);
    for(i=0;str[i]!='\0';i++)
    {
    if(str[i]==' ')
        count++;
    }
    printf("\nThe given sentence has %d words",count+1);
    getch();
}


Sample Output:


Programs and Solutions