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.
7 Réponses
+ 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‼️
+ 3
In C++:
string name = "Somebody";
char arr[name.size() +1];
strcpy(arr, name.c_str());
in JS:
name = "Somebody";
arr = name.split("");
+ 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.
+ 1
the arrays is really poerful and we need them for a lot of things so dont miss them out
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
0
Never mind I figured it out xD