Split a string into an array in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Split a string into an array in c++?

Ok, In JS, we're able to split a variable into an array, right? Is there something like this in c++? I want to split a string into an array.

30th Oct 2018, 1:26 AM
Daniel Cooper
Daniel Cooper - avatar
7 Answers
+ 4
Daniel Cooper ...bcs the numbering of elements in arrays, matrices, lists, tuplets etc in almost? all programming languages ​​we start to index from 0‼️
21st Jan 2020, 8:42 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 3
In C++: string name = "Somebody"; char arr[name.size() +1]; strcpy(arr, name.c_str()); in JS: name = "Somebody"; arr = name.split("");
30th Oct 2018, 2:25 AM
Calviղ
Calviղ - avatar
30th Oct 2018, 2:51 AM
Calviղ
Calviղ - avatar
+ 1
I just want to point out that a string object of std::string is pretty much a vector of chars, and therefore already has array-like properties. For example, if you have a std::string s ("Whatever"); then you can access single characters using square brackets, just like with an array: s[0]; // W s[4]; // e As such, in a more modern C++ style, you can always quickly construct a vector from a string using std::vector<char> vec { s.begin(), s.end() }; But if you just want a plain, simple C-style array, then you can go with Calvin's solution.
30th Oct 2018, 6:16 AM
Shadow
Shadow - avatar
+ 1
the arrays is really poerful and we need them for a lot of things so dont miss them out
1st Nov 2018, 5:51 AM
spyross spyros
spyross spyros - avatar
0
https://code.sololearn.com/cUdQS8LpHb48/?ref=app Why is the size 12 but the last character is at 11? That's so confusing xD
30th Oct 2018, 4:42 PM
Daniel Cooper
Daniel Cooper - avatar
0
Never mind I figured it out xD
30th Oct 2018, 4:43 PM
Daniel Cooper
Daniel Cooper - avatar