C Programming Code Examples C > Strings Code Examples Program to Count Number of Words in a given Text or Sentence Program to Count Number of Words in a given Text or Sentence This program takes a string as input and count the number of words in the input string. - Take a string as input and store it in the array s[]. - Using for loop search for a space ' ' in the string and consecutively increment a variable count. - Do step-2 until the end of the string. - Increment the variable count by 1 and then print the variable count as output. #include <stdio.h> #include <string.h> void main() { char s[180]; int count = 0, j; printf("enter the string\n"); scanf("%[^\n]s", s); for (j = 0;s[j] != '\0';j++) { if (s[j] == ' ') count++; } printf("number of words in given string are: %d\n", count + 1); }