C Program Count Words in Sentence

This C Program counts the number of Words in a Sentence. This is a part of Mumbai University MCA Colleges C Programs MCA Sem 1.
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();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a Comment