tags:

views:

544

answers:

4

Hello, I'm writing a program that requires a string to be inputed, 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 be default. I would like to keep it that way and avoid char, but will use it if necessary.

Any help would be nice, thank you in advance.

+3  A: 
string a = "hello"; 
cout << a[1];

I hope that explains it

Luca Matteis
i don't feel like it as i'm not here to give tutorials, just answers.
Luca Matteis
For the record, you can also just do: cout << "hello"[1];
AshleysBrain
+1  A: 

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];
}
R Samuel Klatchko
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.
Andreas Bonini
+5  A: 

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.

Ryan
A: 

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;
}
Thomas Matthews
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.
George B.
George, sizeof(char) is _always_ 1.
paxdiablo
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.
Ryan
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.
paxdiablo
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.
Ryan
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).
paxdiablo