Count Number of words in a Sentence
Here you will learn an example code to count number of words in a sentence using C++ programming.
What is word definition?
A word is a single unit of language that can be combined with other words to form a sentence.
What is sentence ?
A sentence is a group of words that are put together to make a complete thought or idea.
Example of C++ program to Count Number of words in a Sentence
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> #include <string> using namespace std; int main() { string s; int count = 0; cout << "Enter a big sentence: "; getline(cin, s); for (int i = 0; i < s.length(); i++) { if (s[i] == ' ') count++; } cout << "Number of words in given sentence is: " << count + 1; return 0; } |
Output
Enter a big sentence: Hi i am happy to read code from code revise
Number of words in given sentence is: 10
Check out our other C++ programming Examples