C Programming Code Examples C > Strings Code Examples C program to count number of words in a string C program to count number of words in a string Write a C program to count total number of words in a string using loop. How to find total number of words in a given string using loops in C programming. To count total number of words in a string we just need to count total number of white spaces. White space characters includes single blank space ' ', Tab \t, New line \n. Algorithm to find total number of words in a string %%Input : text {Array of characters /String} N {Size of the string} Begin: words = 0; For j = 0 to N do If (text [j] == ' ', 't', 'n') then word = word + 1; End if End for End #include <stdio.h> #define maxsize 100 // Maximum string size int main() { char str[maxsize]; int j, words; /* Input string from user */ printf("Enter any string: "); gets(str); j = 0; words = 1; /* Runs a loop till end of string */ while(str[j] != '\0') { /* If the current character(str[j]) is white space */ if(str[j]==' ' || str[j]=='\n' || str[j]=='\t') { words++; } j++; } printf("Total number of words = %d", words); return 0; }