This is the program to count number of words in the entered sentence.
Logic: The logic behind this program is checking if the character in the sentence is a ‘blank’ and with the conditions that the next is not a blank character, or current character position is not equal to length of the sentence. If so, variable ‘words’ is incremented by one as one new word found.
int words = 0, len = 0, i;
printf(“\n\n\t ENTER A STRING…: “);
if ( ( str[i] == ‘ ‘ && str[i+1] != ‘ ‘ ) || i == len )
printf(“\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS…: %d”, words);
Download exe and source code here.
Leave a Reply