3

How to split a string into an array of strings for every character? Example:

INPUT:
string text = "String.";

OUTPUT:
["S" , "t" , "r" , "i" , "n" , "g" , "."]

I know that char variables exist, but in this case, I really need an array of strings because of the type of software I'm working on.

When I try to do this, the compiler returns the following error:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0413   no suitable conversion function from "std::string" to "char" exists 

This is because C++ treats stringName[index] as a char, and since the array is a string array, the two are incopatible. Here's my code:

string text = "Sample text";
string process[10000000];

for (int i = 0; i < sizeof(text); i++) {
    text[i] = process[i];
}

Is there any way to do this properly?

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
  • 1
    Shouldn't the code example use `process[i] = text[i]`, assuming that `text` is your input string you want to chop up in the way you describe..? – saxbophone Jul 24 '22 at 13:45
  • 1
    Another code issue, you can't use `sizeof()` to get the length of string as number of characters, you need to call `str.length()` – saxbophone Jul 24 '22 at 13:46
  • A side note: it is recomended in C++ to use `std::vector` instead of raw C arrays. – wohlstad Jul 24 '22 at 13:48
  • Although using vectors is preferred by the community, this is very unlikely to be the cause of the problems reported for the code sample given – saxbophone Jul 24 '22 at 13:49
  • 1
    @saxbophone I totally agree. Meant it as a side note (and updated my comment accordingly). – wohlstad Jul 24 '22 at 13:50
  • 1
    The sub problem that you need to solve is how to create an object of type `std::string` that holds a single character. When you’ve got that, repeat until done. – Pete Becker Jul 24 '22 at 13:50
  • 1
    Another thing you could do is get an array that's double the length of the source array, zero it, and then copy to every other position, such that '\0' ends up separating every character. There, now you have your strings (without needing to create whole C++ strings, but you can if you need to). – Mona the Monad Jul 24 '22 at 13:51
  • [Sorry for writing this in the comment section, but I solved it and want to share] I recommend you to not use such a big array (10 million) of strings:) I change just a little bit your code and get what you want: `string text = "Sample text"; string process[text.length()]; for (int i = 0; i < text.length(); i++) { process[i] = text[i]; }` – Tim Shidlovskii Jul 24 '22 at 13:55
  • 1
    I looks like the question this was marked as a duplicate of was an irrelevant choice of duplicate, (flagged). – saxbophone Jul 24 '22 at 13:56
  • @saxbophone yes. I have tried to publish it as an answer, but someone marked this question as a duplicate, and now I'm unable to write an answer. – Tim Shidlovskii Jul 24 '22 at 14:00
  • 1
    @saxbophone it is some kind of irony that what OP highlighted in the part "why it's not a duplicate" was probably mistakenly read as a duplicate – The Dreams Wind Jul 24 '22 at 14:01
  • 3
    It doesn't really help to start with the problem you are not trying to solve, but anyway reopened, the duplicate was incorrect. – john Jul 24 '22 at 14:03

2 Answers2

2

If you are going to make string, you should look at the string constructors. There's one that is suitable for you (#2 in the list I linked to)

for (int i = 0; i < text.size(); i++) {
    process[i] = string(1, text[i]); // create a string with 1 copy of text[i]
}

You should also realise that sizeof does not get you the size of a string! Use the size() or length() method for that.

You also need to get text and process the right way around, but I guess that was just a typo.

john
  • 85,011
  • 4
  • 57
  • 81
2

std::string is a container in the first place, thus anything that you can do to a container, you can do to an instance of std::string. I would get use of the std::transform here:

const std::string str { "String." };
std::vector<std::string> result(str.size());
std::transform(str.cbegin(), str.cend(), result.begin(), [](auto character) {
    return std::string(1, character);
});
The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49