Problem statement
Take a string from user and count number of words in a string. And then return count to user as an output.
Program to count words in string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> #include<string.h> int main() { char a[60]; int i=0,count=0; printf("Enter the string\n"); gets(a); for(i=0;a[i]!='\0';i++) { if(a[i]==' ') { count++; } } printf("Number of words in a string is %d",count+1); } |
Output
1 2 3 |
Enter the string C is a programming language Number of words in a string is 5 |