0
How can I separate words in a string in c++?
Can you give an easy way to separate words in a c++ string using a for loop?
5 Answers
+ 3
// Hope this code helps you
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string tmp, cars = "Volvo BMW Ford Mazda";
stringstream ss(cars);
while(getline(ss, tmp, ' ')) cout << tmp << endl;
return 0;
}
https://code.sololearn.com/ciggAQsje69O
+ 2
// Hope this code helps you
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for (string car : cars) {
// printf("%s ", car.c_str());
cout << car + " ";
}
+ 1
ss is a variable name in the type stringstream
and the idea of the code is split.
0
Can you explain me the second code because I didn't understand the "ss" and other things on it please?
0
And why do you use the ' '?