How to split the strings into arrays in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to split the strings into arrays in C++?

Assume that a string contains characters or words with whitespaces with between them. Is there any way to split them without using vector?

16th Aug 2017, 3:36 PM
Hamidi Saufi
Hamidi Saufi - avatar
4 Answers
+ 7
Actually, you need a parser to split the words based on a delimiter and then storing each splitted word (token) into an array or vector . Take a look at this post at StackOverflow https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
16th Aug 2017, 3:45 PM
Babak
Babak - avatar
+ 6
You still need a parser dear. But it might become more complex as you expect more from that.
16th Aug 2017, 4:32 PM
Babak
Babak - avatar
+ 1
edit: my answer here isn't the best solution and it remained after trying to delete it. you could use another string to work as a buffer. so string buffer; int j = 0; for (int i = 0; i < sentence.length(); i++) { if (string[i] == space) { //use j and i to add the selected word; j = i + 1; } }
16th Aug 2017, 3:47 PM
Christian
Christian - avatar
0
Ok another question is that, how do we split the string that the user has enter an input in it?
16th Aug 2017, 4:20 PM
Hamidi Saufi
Hamidi Saufi - avatar