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?

22nd Jun 2022, 9:05 PM
Basma M
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
22nd Jun 2022, 9:29 PM
SoloProg
SoloProg - avatar
+ 2
// Hope this code helps you string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"}; for (string car : cars) { // printf("%s ", car.c_str()); cout << car + " "; }
22nd Jun 2022, 9:21 PM
SoloProg
SoloProg - avatar
+ 1
ss is a variable name in the type stringstream and the idea of the code is split.
1st Jul 2022, 10:04 AM
SoloProg
SoloProg - avatar
0
Can you explain me the second code because I didn't understand the "ss" and other things on it please?
22nd Jun 2022, 9:36 PM
Basma M
0
And why do you use the ' '?
1st Jul 2022, 6:28 PM
Basma M