Skip to content
doubtly

1 answer

Best answer

#include <iostream>
#include <string>

using namespace std;

int main() {
 string line;
 int count = 0;

 cout << "Enter a line of text: ";
 getline(cin, line);

 for (int i = 0; i < line.length(); i++) {
 if (line[i] == ' ') {
 count++;
 }
 }

 cout << "Number of words in the line: " << count + 1 << endl;

 return 0;
}

Doubtly · 19 Mar 2023 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.