C++ Program to Count Words in String with Explanation
C++
Medium
Strings
36 views
1 min read
199 words
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around word, sentence, count. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Count total number of words in a sentence.
Real Life: Word count in document editors.
Input Format
A full sentence (string with spaces).
Output Format
Total number of words in the sentence.
Constraints
• Sentence length ≥ 1
• Words are separated by one or more spaces
• Ignore leading and trailing spaces
Concept Explanation
A word is a group of characters separated by spaces.
We count how many such groups exist.
This is similar to word count in document editors.
Step-by-Step Logic
1.Take the full sentence as input.
2.Initialize count = 0.
3.Traverse the sentence character by character.
4.Detect the start of a word when:
• Current character is not a space
• Previous character is a space (or it is the first character)
5.When a word start is found, increment count.
6.Continue until the end of the sentence.
7.Print count.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
void string_q8_count_words() {
string text = "C plus plus is awesome";
int wordCount = 0;
bool inWord = false;
for(int i = 0; i < text.length(); i++) {
if(text[i] != ' ' && !inWord) {
wordCount++;
inWord = true;
} else if(text[i] == ' ') {
inWord = false;
}
}
cout << "String: " << text << endl;
cout << "Total words: " << wordCount << endl;
}
Output Example
Input:
I love competitive programming
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Count total number of words in a sentence.
Real Life: Word count in document editors.
Input / Output
Input
A full sentence (string with spaces).
Output
Total number of words in the sentence.
Constraints
• Sentence length ≥ 1
• Words are separated by one or more spaces
• Ignore leading and trailing spaces
Examples
Input:
I love competitive programming
Explanation
Concept Explanation
A word is a group of characters separated by spaces.
We count how many such groups exist.
This is similar to word count in document editors.
Step-by-Step Explanation
1.Take the full sentence as input.
2.Initialize count = 0.
3.Traverse the sentence character by character.
4.Detect the start of a word when:
• Current character is not a space
• Previous character is a space (or it is the first character)
5.When a word start is found, increment count.
6.Continue until the end of the sentence.
7.Print count.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
void string_q8_count_words() {
string text = "C plus plus is awesome";
int wordCount = 0;
bool inWord = false;
for(int i = 0; i < text.length(); i++) {
if(text[i] != ' ' && !inWord) {
wordCount++;
inWord = true;
} else if(text[i] == ' ') {
inWord = false;
}
}
cout << "String: " << text << endl;
cout << "Total words: " << wordCount << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!