Split string into array of chars
74,673
Solution 1
string a = "hello";
cout << a[1];
I hope that explains it
Solution 2
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.
Solution 3
A string is just a sequence of the underlying character (i.e. char for std::string and wchar_t for std::wstring).
Because of that, you easily get each letter:
for (std::string::size_type l = 0; l < str.length(); ++l)
{
std::string::value_type c = str[l];
}
Solution 4
Try using the c_str()
method of std::string
:
#include <string>
using namespace std;
int main(void)
{
string text = "hello";
size_t length = text.length() + sizeof('\0');
char * letters = new char[length];
strcpy(letters, length.c_str());
for (unsigned int i = 0; i < length; ++i)
{
cout << '[' << i << "] == '" << letters[i] << "'\n";
}
return EXIT_SUCCESS;
}
Author by
Galileo
Updated on July 17, 2022Comments
-
Galileo almost 2 years
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. -
Andreas Bonini over 14 yearsYou're::gonna::scare::him::with::all::that::stuff. imo using
int
or, even better,size_t
will work on every existing platform and it's way more clear. -
George over 14 yearsAdding sizeof('\0') to length isnt needed because it may be more than 1 and then the allocated array will have a higher size than needed. Adding 1 instead of sizeof('\0') is better.
-
paxdiablo over 14 yearsGeorge, sizeof(char) is always 1.
-
Ryan over 14 yearsNot true! sizeof(char) is not always 1. I worked with a DSP chip where everything was at least 32-bit, char, short, int -- sizeof(char) == sizeof(short) == sizeof(int). The C standard just says that sizeof(char) <= sizeof(short) <= sizeof(int) but does not require sizeof(char) == 1. One of the side effects was that casting to char and BYTE to limit values to 8-bits had no effect since the data type was natively 32-bit. Granted this was an unusual architecture, but the C standard definitely does not say sizeof(char) == 1.
-
Luca Matteis over 14 yearsi don't feel like it as i'm not here to give tutorials, just answers.
-
paxdiablo over 14 yearsNo, @Ryan, I'm sorry but you're wrong. Section 5.3.3 "Sizeof" states: "sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1". A char is a byte but neither is necessarily an octet (8 bits). This is the same as with C. This is from the n3000 draft of C++0x but it's been that way for ages.
-
AshleysBrain over 14 yearsFor the record, you can also just do: cout << "hello"[1];
-
Ryan over 14 yearsAh, I stand corrected. My real point was do not assume 1 == 8 bits which you clearly state. The interesting thing about the DSP chip was 1 == sizeof(char) == sizeof(short) == sizeof(int) which surprised me when I found that out where 1 == 32 bits.
-
paxdiablo over 14 yearsYeah, Ryan, that actually bit me once. I thought I'd found a discrepancy in the ISO C standard since they talked about a char being allowed to be more than 8 bits yet sizeof returned the number of bytes (but still one for char). Turns out ISO had redefined byte as well (which makes sense, all other standards, like their comms ones, use octet for an 8-bit value).
-
Yakov Galka over 13 years@AshleysBrain: Or cout << 1["hello"];
-
Fomentia over 8 yearsBest solution I've seen! Thank you very much.