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;
}
Share:
74,673
Galileo
Author by

Galileo

Updated on July 17, 2022

Comments

  • Galileo
    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
    Andreas Bonini over 14 years
    You'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
    George over 14 years
    Adding 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
    paxdiablo over 14 years
    George, sizeof(char) is always 1.
  • Ryan
    Ryan over 14 years
    Not 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
    Luca Matteis over 14 years
    i don't feel like it as i'm not here to give tutorials, just answers.
  • paxdiablo
    paxdiablo over 14 years
    No, @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
    AshleysBrain over 14 years
    For the record, you can also just do: cout << "hello"[1];
  • Ryan
    Ryan over 14 years
    Ah, 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
    paxdiablo over 14 years
    Yeah, 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
    Yakov Galka over 13 years
    @AshleysBrain: Or cout << 1["hello"];
  • Fomentia
    Fomentia over 8 years
    Best solution I've seen! Thank you very much.