C program to count the words in a line.
Count the total number of words in a line.
In this post, I had shared a C program that will count the number of words in the line you had inserted.
Let us observe the code :)
#include <stdio.h>
#include <conio.h>
void main()
{
char s[50],ch;
int i,c=0;
clrscr();
printf("Enter the string: ");
for(i=0;ch!='\n';i++)
{
ch=getchar();
s[i]=ch;
}
s[i]='\0';
for(i=0;s[i]!='\0';i++)
{
if(s[i]==' ')
{
c++;
while(s[i]==' ')
i++;
}
}
c++;
printf("\nTotal words: %d",c);
getch();
}
Comments
Post a Comment