2

I'm trying to find a way to convert a string to array of c strings. So for example my string would be:

std::string s = "This is a string."

and then I would like the output to be something like this:

array[0] = This
array[1] = is
array[2] = a
array[3] = string.
array[4] = NULL
7
  • 1
  • 11
    That is an array of strings, not characters. Commented Feb 12, 2013 at 1:06
  • So you have an array of std::strings? Like std::string strings[5];? Commented Feb 12, 2013 at 1:06
  • 2
    You're splitting a string on whitespaces, not converting to a character array. That first link works, or you can search 'splitting a string c++' to find what you want. Commented Feb 12, 2013 at 1:07
  • 1
    Removed the C tag, as the question is meaningless in C. In C, a string is an array of characters, so there's no conversion to be done. Commented Feb 12, 2013 at 1:08

3 Answers 3

4

You are trying to split string into strings. Try:

 #include <sstream>
 #include <vector>
 #include <iostream>
 #include <string>

 std::string s = "This is a string.";

  std::vector<std::string> array;
  std::stringstream ss(s);
  std::string tmp;
  while(std::getline(ss, tmp, ' '))
  {
    array.push_back(tmp);
  }

  for(auto it = array.begin(); it != array.end(); ++it)
  {
    std::cout << (*it) << std:: endl;
  }

Or see this split

3
  • I tried using your example but I get this "error: variable 'std::stringstream ss' has initializer but incomplete type"
    – Peter T.
    Commented Feb 12, 2013 at 1:41
  • you need to include necessary headers, see my updated answer
    – billz
    Commented Feb 12, 2013 at 1:42
  • Ahh, I had the other 3 I was just missing the sstream. Thanks a lot, this has been a big help.
    – Peter T.
    Commented Feb 12, 2013 at 1:47
1

Split your string into multiple strings based on a delimiter using the Boost library function 'split' like this:

#include <boost/algorithm/string.hpp>
std::vector<std::string> strs;
boost::split(strs, "string to split", boost::is_any_of(" "));

And then iterate over the strs vector.

This approach allows you to specify as many delimiters as you like.

See here for more: http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768

And there is a plethora of approaches here: Split a string in C++?

-2

On your example.

The array is not an array of characters, it is an array of strings.

Well, actually, a string is an array of characters.

//Let's say:
string s = "This is a string.";
//Therefore:
s[0] = T
s[1] = h
s[2] = i
s[3] = s

But based on your example,

I think you want to Split the text. (with SPACE as delimeter).

You can use the Split function of the String.

string s = "This is a string.";
string[] words = s.Split(' ');
//Therefore:
words[0] = This
words[1] = is
words[2] = a
words[3] = string.
1
  • 3
    You're going to have to divulge which C++ standard dictates the Split() member of std::string. This isn't Java.
    – WhozCraig
    Commented Feb 12, 2013 at 1:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.