C Program to Count Words in Sentence

Ram Pothuraju
This C Program counts the number of Words in a Sentence. 

The words are counted by counting the character " " between 2 chars. 


#include<stdio.h>
#include<conio.h>
void main()
{
 int len,i,cntword=0;
 char sent[100];
 clrscr();
 printf("Enter a sentence\n");
 gets(sent);
 for(len=0;sent[len]!='\0';len++);
 for(i=0;i<=len;i++)
 {
  if(sent[i]==' '||sent[i]=='\0'||sent[i]=='\t')
  {
   cntword++;
   while(sent[i+1]==' '||sent[i+1]=='\0'||sent[i+1]=='\t')
    i++;
  }
 }
 printf("Number of word in a sentence are = %d",cntword);
 getch();
}

Tags

Post a Comment

0Comments

Post a Comment (0)