C Program to Count Words in a String
if ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') && (i == 0 || str[i-1] == ' ' || str[i-1] == '\t' || str[i-1] == '\n')) count++;.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> #include <string.h> int main() { char str[1000]; int count = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); for (int i = 0; str[i] != '\0'; i++) { if ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') && (i == 0 || str[i-1] == ' ' || str[i-1] == '\t' || str[i-1] == '\n')) { count++; } } printf("Number of words: %d\n", count); return 0; }
Dry Run
Let's trace the input "Hello world" through the code
Start scanning string
str = "Hello world\n"; count = 0; i = 0
Check first character 'H'
'H' is not space and i=0, so count = 1
Scan characters until space
Characters 'e', 'l', 'l', 'o' are not spaces but previous chars are not spaces, so count stays 1
At space character
str[5] = ' ' (space), no count increase
Next character 'w'
'w' is not space and previous char is space, count = 2
Continue scanning rest
Characters 'o', 'r', 'l', 'd' no count increase
End of string
Total count = 2
| Index | Character | Previous Character | Count |
|---|---|---|---|
| 0 | H | N/A | 1 |
| 1 | e | H | 1 |
| 2 | l | e | 1 |
| 3 | l | l | 1 |
| 4 | o | l | 1 |
| 5 | o | 1 | |
| 6 | w | 2 | |
| 7 | o | w | 2 |
| 8 | r | o | 2 |
| 9 | l | r | 2 |
| 10 | d | l | 2 |
| 11 | \n | d | 2 |
Why This Works
Step 1: Identify word start
A word starts when a non-space character follows a space or is at the string start, so we check these conditions to count words.
Step 2: Ignore multiple spaces
By only counting when previous character is space, we avoid counting spaces or multiple spaces as words.
Step 3: Use loop to scan string
Looping through each character lets us check every position for word starts efficiently.
Alternative Approaches
#include <stdio.h> #include <string.h> int main() { char str[1000]; int count = 0; char *token; printf("Enter a string: "); fgets(str, sizeof(str), stdin); token = strtok(str, " \t\n"); while (token != NULL) { count++; token = strtok(NULL, " \t\n"); } printf("Number of words: %d\n", count); return 0; }
#include <stdio.h> int main() { char str[1000]; int count = 0, inWord = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); for (int i = 0; str[i] != '\0'; i++) { if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n') { if (!inWord) { count++; inWord = 1; } } else { inWord = 0; } } printf("Number of words: %d\n", count); return 0; }
Complexity: O(n) time, O(1) space
Time Complexity
The program scans the string once, so time grows linearly with string length.
Space Complexity
Only a few variables are used, so space is constant regardless of input size.
Which Approach is Fastest?
The direct scan with conditions is fastest and simplest; strtok is convenient but modifies input.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct scan with conditions | O(n) | O(1) | Simple and fast word counting |
| strtok tokenization | O(n) | O(1) | When modifying input is acceptable |
| Flag tracking method | O(n) | O(1) | Clear logic for word boundaries |