Split with stringstream
Completed
Read a line of words, split it with istringstream, and print the number of words and the longest word (first on ties).
Input
One line.
Output
Two lines.
Example
Input
the quick brown fox
Output
4 quick
Editor
C++
Hints
An istringstream lets you use >> on a string.
while (in >> word) stops at the end of the string.
Solution explanation
A string stream has a real end, so looping until extraction fails is safe here.
Show solution
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string line, word, longest;
getline(cin, line);
istringstream in(line);
int count = 0;
while (in >> word) {
count++;
if (word.size() > longest.size()) longest = word;
}
cout << count << "\n" << longest << "\n";
return 0;
}