+1 vote
192 views
in Programming by (98.9k points)
edited
Write a program in C++, to count the number of words in a line of text

1 Answer

0 votes
by (98.9k points)
selected by
 
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;
}

Related questions

0 votes
0 answers 152 views
0 votes
1 answer 1.5k views
+1 vote
1 answer 247 views
0 votes
1 answer 354 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

557 users

...