Count Word and Character in String – C Program
Write a C Program to count number of word and character of given string.
#include<stdio.h>
int main()
{
int cword=0, cchar=0, i;
char str[50];
printf("Please, Enter the string = ");
gets(str);
for(i=0; str[i]!=NULL; i++)
{
cchar++;
if(str[i]==' ')
{
cwords++;
}
}
printf("\nThe Number of characters = %d",cchar);
printf("\nThe Number of words = % d",cwords+1);
return 0;
}
The output of count word and character c program is :
Please, Enter the string = My name is meera
The Number of characters = 16
The Number of words = 4