Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into array of chars

Tags:

I'm writing a program that requires a string to be inputted, then broken up into individual letters. Essentially, I need help finding a way to turn "string" into ["s","t","r","i","n","g"]. The strings are also stored using the string data type instead of just an array of chars by default. I would like to keep it that way and avoid char but will use it if necessary.

like image 963
Galileo Avatar asked Jan 29 '10 00:01

Galileo


People also ask

How do you split a string into an array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you split a string into an array of letters in Python?

To convert String to array in Python, use String. split() method. The String . split() method splits the String from the delimiter and returns the splitter elements as individual list items.

How do you split a string into characters?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

How do I split a string into a list in Word?

To convert a string in a list of words, you just need to split it on whitespace. You can use split() from the string class. The default delimiter for this method is whitespace, i.e., when called on a string, it'll split that string at whitespace characters.


2 Answers

Assuming you already have the string inputted:

string s("string");
vector<char> v(s.begin(), s.end());

This will fill the vector v with the characters from a string.

like image 101
Ryan Avatar answered Oct 14 '22 08:10

Ryan


string a = "hello"; 
cout << a[1];

I hope that explains it

like image 28
Luca Matteis Avatar answered Oct 14 '22 08:10

Luca Matteis