How to separate an string to it's words? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

How to separate an string to it's words?

Do you have some other solution different from this? https://code.sololearn.com/cv7nO62LSZA8/?ref=app

13th Mar 2020, 9:41 AM
Mohammad Aghazadeh
Mohammad Aghazadeh - avatar
5 Answers
+ 7
~ swim ~ thanks
13th Mar 2020, 10:08 AM
Mohammad Aghazadeh
Mohammad Aghazadeh - avatar
+ 7
Maxim Thank you
14th Mar 2020, 5:36 PM
Mohammad Aghazadeh
Mohammad Aghazadeh - avatar
+ 4
rodwynnejones That was helpful. Thanks.
14th Mar 2020, 10:46 PM
Mohammad Aghazadeh
Mohammad Aghazadeh - avatar
+ 2
If you need only separated output #include <iostream> #include <string> using namespace std; int main() { int unsigned i; string input="I love sololearn application!"; for (i=0; i<input.length (); i++) { if (input[i]!=' '){ cout << input[i]; } else { cout << endl; } } return 0; }
14th Mar 2020, 5:32 PM
Maxim
Maxim - avatar
+ 2
Here my version using strtok from cstring header:- #include <iostream> #include <string> #include <cstring> using namespace std; int main(){ string phrase = "I love sololearn application too!"; char *word = strtok(const_cast<char *>(phrase.c_str()), " "); while(word){ cout << word << endl; word = strtok(NULL, " "); } return 0; } ahh...just realised.......post is two days old...oh well...never mind.
14th Mar 2020, 10:10 PM
rodwynnejones
rodwynnejones - avatar