24

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.

3
  • Do you need to do something to the letters after it's split? That is, you need to put the letters into some kind of data structure other than the string? Commented Jan 29, 2010 at 0:15
  • This is a silly question, a string is an array of chars. Commented Jan 20, 2012 at 21:40
  • Thanks for the question, the answer helped me, I know it's obvious but I was stuck. Commented Dec 11, 2021 at 16:26

7 Answers 7

35

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.

Sign up to request clarification or add additional context in comments.

Comments

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

I hope that explains it

3 Comments

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

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];
}

1 Comment

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.
1

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;
}

6 Comments

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, sizeof(char) is always 1.
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.
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.
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.
|
0
string input ="some string for example my cat is handsome";
vector<string> split_char_to_vector(string input) {
  vector<string> output;
  for(size_t i=0;i<=input.length();i++) {
    output.push_back(input[i]));
  }
  return output;
}

if you want to convert split strings into character the first traverse the string and write for each characters of string to the i'th position of char array ie

 char array[1000];
std::string input="i dont think love never ends";
 for(size_t i=0;i<=input.length();/*or string::npos;*/i++)
  {
            if (input[i] != '\0')
        {
            array[i] = input[i];
        }
        else
        {
            break;
        }
    }
    for (size_t i = 0; i < 100; i++)
    {
        std::cout << array[i] << std::endl;
    }

2 Comments

Add the details also to make answer understandable.
Welcome to Stack Overflow! While this code may answer the question, it is better to include some context, explaining how it works and when to use it. Code-only answers tend to be less useful in the long run. See How do I write a good answer? for some more info.
0

if you want to convert split strings into character the first traverse the string and write for each characters of string to the i'th position of char array ie

  char array[1000];
   std::string input="i dont think love never ends";
   for(size_t i=0;i<=input.length();/*or string::npos;*/i++)
    {
        if (input[i] != '\0')
        {
        array[i] = input[i];
        }
    else
        {
        break;
        }
    }
    //to avoid noise after position input.length();
        for (size_t i = input.length(); i <= 1000; i++)
        {
             array[i] = '\0';
        }
      //ie return array; or print
    for (size_t i = 0; i < 100; i++)
    {
    std::cout << array[i] << std::endl;
    }

Comments

0

You can use a for loop to iterate through the characters of a string.

    std::string str = "word"
    for(char i : str){
        std::cout << i << std::endl;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.