Count Words in a Sentence: flowchart, algorithm and program
This program reads a sentence with fgets() and counts how many words it has. Instead of looking at the words themselves, it counts the spaces that sit between two words and then adds 1 for the first word.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: i, count = 0.
- Step 3: Read sentence.
- Step 4: Set i = 0.
- Step 5: If sentence[i] not '\0' and not '\n', go to step 6; otherwise go to step 9.
- Step 6: If sentence[i] == ' ' and next char is not ' ', '\0', '\n', go to step 7; otherwise go to step 8.
- Step 7: Increase count by 1.
- Step 8: Increase i by 1. Go to step 5.
- Step 9: If sentence[0] not '\0', '\n', ' ', go to step 10; otherwise go to step 11.
- Step 10: Increase count by 1.
- Step 11: Print count.
- Step 12: Stop.
Pseudocode
BEGIN
i, count ← 0
READ sentence
i ← 0
WHILE sentence[i] not '\0' and not '\n' DO
IF sentence[i] == ' ' and next char is not ' ', '\0', '\n' THEN
count++
END IF
i++
END WHILE
IF sentence[0] not '\0', '\n', ' ' THEN
count++
END IF
PRINT count
END
Program in C, Python and Java
Count Words in a Sentence program in C
#include <stdio.h>
int main() {
char sentence[200];
int i, count = 0;
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);
for (i = 0; sentence[i] != '\0' && sentence[i] != '\n'; i++) {
if (sentence[i] == ' ' && sentence[i + 1] != ' ' && sentence[i + 1] != '\0' && sentence[i + 1] != '\n') {
count++;
}
}
if (sentence[0] != '\0' && sentence[0] != '\n' && sentence[0] != ' ') {
count++;
}
printf("Number of words: %d\n", count);
return 0;
}Count Words in a Sentence program in Python
sentence = input("Enter a sentence: ")
count = 0
for i in range(len(sentence)):
if sentence[i] == ' ' and i + 1 < len(sentence) and sentence[i + 1] != ' ':
count += 1
if len(sentence) > 0 and sentence[0] != ' ':
count += 1
print("Number of words:", count)Count Words in a Sentence program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
int count = 0;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ' ' && i + 1 < sentence.length() && sentence.charAt(i + 1) != ' ') {
count++;
}
}
if (sentence.length() > 0 && sentence.charAt(0) != ' ') {
count++;
}
System.out.println("Number of words: " + count);
}
}
Sample input: sentence = Programming is fun to learn
Output: Number of words: 5
How it works
- fgets() reads the whole line into sentence, and count starts at 0.
- The for loop walks through sentence with i and stops at '\0' or '\n', so the end of the line is never treated as a word.
- A space is counted only if sentence[i] == ' ' and the next character is not a space, '\0' or '\n'. This is a space that really sits between two words, so repeated spaces and trailing spaces are not counted.
- After the loop, count++ runs once if the sentence does not start with a space or a newline. This adds the first word, because words = separators + 1.
- For "Programming is fun to learn", 4 spaces are counted and the first word adds 1, so the output is 5.
Dry run (trace table)
| Step | Line | Box | i | count | sentence | Output |
|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||
| 2 | 5 | i, count = 0 | ? | 0 | ||
| 3 | 8 | Read sentence | ? | 0 | Programming is fun to learn\n | |
| 4 | 10 | i = 0 | 0 | 0 | ||
| 5 | 10 | sentence[i] not '\0' and not '\n'? | 0 | 0 | ||
| 6 | 11 | sentence[i] == ' ' and next char is not ' ', '\0', '\n'? | 0 | 0 | ||
| 7 | 10 | i++ | 1 | 0 | ||
| 8 | 10 | sentence[i] not '\0' and not '\n'? | 1 | 0 | ||
| 9 | 11 | sentence[i] == ' ' and next char is not ' ', '\0', '\n'? | 1 | 0 | ||
| 10 | 10 | i++ | 2 | 0 | ||
| 11 | 10 | sentence[i] not '\0' and not '\n'? | 2 | 0 | ||
| 12 | 11 | sentence[i] == ' ' and next char is not ' ', '\0', '\n'? | 2 | 0 | ||
| 13 | 10 | i++ | 3 | 0 | ||
| 14 | 10 | sentence[i] not '\0' and not '\n'? | 3 | 0 | ||
| 15 | 11 | sentence[i] == ' ' and next char is not ' ', '\0', '\n'? | 3 | 0 | ||
| 16 | 10 | i++ | 4 | 0 | ||
| 17 | 10 | sentence[i] not '\0' and not '\n'? | 4 | 0 | ||
| 18 | 11 | sentence[i] == ' ' and next char is not ' ', '\0', '\n'? | 4 | 0 | ||
| 19 | 10 | i++ | 5 | 0 | ||
| 20 | 10 | sentence[i] not '\0' and not '\n'? | 5 | 0 |
Time and space complexity
Time: O(n) · Space: O(1). The loop visits each character of the sentence once and does constant work per character, using only a few extra variables.
Common mistakes
- Forgetting the extra count++ for the first word. The spaces give 4 for a 5-word sentence, so the answer comes out one short.
- Counting every space without checking the next character. Double spaces or a space at the end of the line then give a wrong, larger count.
- Not stopping at '\n'. fgets() keeps the newline in the string, so a trailing newline can be mistaken for part of the sentence.
Viva questions
Q1. Why is count initialised to 0, and what does it hold at the end?
count starts at 0 because no words have been seen yet. At the end it holds the number of words: one for each space followed by a real character, plus one for the first word.
Q2. Why do you use fgets instead of scanf("%s") to read the sentence?
scanf with %s stops at the first space, so it would read only one word. fgets reads the whole line including spaces, and it limits input to sizeof(sentence) so the buffer cannot overflow.
Q3. Why does the loop condition check for both '\0' and '\n'?
'\0' marks the end of the string, and fgets also stores the newline when the user presses Enter. Checking both stops the loop at the real end of the sentence, so the newline is not treated as part of it.
Q4. Explain the condition on line 11. Why check sentence[i+1] as well as sentence[i]?
A word is counted when a space is followed by a non-space character, which marks the start of a new word. Checking the next character means multiple spaces in a row and trailing spaces are not counted as extra words.
Q5. What is the purpose of the if on line 16? What input would give a wrong answer without it?
The loop only counts words that come after a space, so the first word is never counted there. Line 16 adds one for it if the sentence does not start with a space or end immediately. Without it, 'hello world' would give 1 instead of 2.
Q6. What does the program print for an empty line, for ' ' (only spaces), and for ' hello world'?
An empty line gives 0, because line 16 fails and the loop does nothing. Only spaces gives 0, because no space is followed by a non-space. For ' hello world' it gives 1 instead of 2: the space before 'hello' is counted, but line 16 is skipped because of the leading space. So leading spaces are handled correctly only by luck of this count. A sentence starting with a space still counts every word once.
Q7. What is the time complexity, and does the input size affect space?
Time is O(n), where n is the length of the sentence, because it makes one pass over the characters with constant work per character. Space is O(1) extra, apart from the fixed 200-character buffer.
Q8. What would you change to also treat tabs as word separators?
Replace the comparisons with ' ' by a helper that also accepts '\t', or use isspace() from <ctype.h>. For example, test isspace(sentence[i]) && !isspace(sentence[i+1]) so spaces, tabs and other whitespace all work.
Frequently asked questions
Why do we add 1 after counting the spaces?
Words are separated by spaces, so a sentence with n words has n-1 spaces between them. Adding 1 for the first word gives the correct total.
What happens if the sentence has extra spaces between words?
They are ignored. A space is counted only when the next character is not a space, so "a b" still gives 2 words.
Does this program work if the sentence starts with a space?
Not fully. The final check skips the +1 when sentence[0] is a space, so " hello world" gives 1 instead of 2. To fix it, skip the leading spaces first, or count the start of each word instead of each space.