My Subject Blog

C Program to Count No of Words in Sentence

/*
Write a program to count no of words in a sentence. 
*/

#include <stdio.h>
int main(void)
{
char str[100];
int i,cnt=0,flag=0;
printf("\n Enter Sentence : ");
scanf("%[^\n]s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
{
flag=0;
}
else if(str[i]!=' ' && flag==0)
{
cnt++;
flag=1;
}
}
printf("\n No of Words in Sentence = %d",cnt);
return 0;
}

Comments

Most Visited Post

C Program to Find Largest Odd Number from Array

C Program for Runtime Memory Allocation for Table of Integers

Followers