开发者

Split string into array of chars

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.


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.


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

I hope that explains it


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


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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜