+ 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
7 Réponses
+ 7
~ swim ~
thanks
+ 7
Maxim
Thank you
+ 4
rodwynnejones
That was helpful. Thanks.
+ 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;
}
+ 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.