Write a program to count a number of words in an entered string.
#include<stdio.h> unsigned int Count_Words_of_Strings(char*); int main() { char s[100]; unsigned int word_count = 0; printf("Enter String = "); scanf("%[^\n]",s); word_count = Count_Words_of_Strings(s); printf("%s contains %d words\n",s,word_count); return 0; } unsigned int Count_Words_of_Strings(char*p) { unsigned int count = 0; int j; j = 0; while(*p) { if(*p == ' ' || *p == '\n' || *p == '\t' || *p == '\0') { j = 0; } else if(j == 0) { j = 1; count++; } p++; } return count; }
Output
Enter String = Lot To Learn Lot To Learn contains 3 words Enter String = BlackBerry Apple Which is Best BlackBerry Apple Which is Best contains 5 words Enter String = Incredible India Incredible India contains 2 words