C Program to Find Number of Characters and Words in a String

include

include

int main()
{
char str[50];
int i=0, word=0, chr=0;
printf(“\nEnter Your String: “);
gets(str);
while (str[i] != ‘\0’)
{
if (str[i] == ‘ ‘)
{
word++;
chr++;
}
else
chr++;
i++;
}
printf(“\nNumber of characters: %d”, chr);
printf(“\nNumber of words: %d”, word+1);
return 0;
}
OUTPUT:
Enter Your String: Welcome to Programming9 .com

Number of characters: 28
Number of words: 4

Leave a Reply

Your email address will not be published. Required fields are marked *