0

I have a sentence, I want split the sentence so that adds each word in an array item.
I have done the following code but it still wrong.

string str = "Welcome to the computer world.";
string strWords[5];
short counter = 0;
for(short i=0;i<str.length();i++){
    strWords[counter] = str[i];
    if(str[i] == ' '){
        counter++;
    }
}
Lion King
  • 32,851
  • 25
  • 81
  • 143
  • 1
    possible duplicate of [How to split a string in C++?](http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c) – Cory Kramer Oct 17 '14 at 12:18
  • Also http://stackoverflow.com/questions/8448176/split-a-string-into-an-array-in-c – Cory Kramer Oct 17 '14 at 12:19
  • @Cyber: My question is slightly different from the questions that you think that my question is similar to them. – Lion King Oct 17 '14 at 12:29
  • 1
    I doubt it. You want to split a string on spaces. You happened to want to add them to an array, but the easier, safer, and more extensible solution would be using a stream to parse it into a `vector`. – Cory Kramer Oct 17 '14 at 12:30
  • Here is my solution with plain C++ code and no fancy functions: https://stackoverflow.com/a/58797347/8209106 – R.singh Nov 11 '19 at 07:51

3 Answers3

3

I'm answering since you should learn from your mistakes: just use the += string operator and your code will work:

// strWords[counter] = str[i];   <- change this
strWords[counter] += str[i];     <- to this

to remove the spaces (if you don't want to append them) just change the order of the space check, something like:

for (short i = 0; i<str.length(); i++){
    if (str[i] == ' ')
        counter++;
    else
        strWords[counter] += str[i];
}

anyway I'm suggesting to use the duplicate link Split a string in C++? too

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
2

Very ugly way to do it, @Cyber has linked to the best answer. But here's your "corrected" version:

string str = "Welcome to the computer world.";
string strWords[5];
short counter = 0;

for(short i=0;i<str.length();i++){
    if(str[i] == ' '){
        counter++;
        i++;
    }
    strWords[counter] += str[i];
}
0

As mentioned in the comments, there are a lot of more convenient ways to split a string (strtok, std functionality, etc.), but if we talk about your sample, you should not assign the 'str[i]' but append it, since it is a single character that you want to append to the current word like this:

string str = "Welcome to the computer world.";
string strWords[5];
short counter = 0;
for(short i=0;i<str.length();i++){
    if(str[i] == ' ' && !strWords[counter].empty()){
        counter++;
    }
    else {
        strWords[counter] += str[i];
    }
}

But this will work only on the given input data, since you can access the array strWords outside bounds if you have more than five words. Consider using a following code:

string str = "Welcome to the computer world.";
vector<string> strWords;
string currentWord;
for(short i=0;i<str.length();i++){
    if(str[i] == ' ' && !currentWord.empty()){
       strWords.push_back(currentWord);
       currentWord.clear();
    }
    else {
        currentWord += str[i];
    }
}

UPDATE

Since I assume you are new to C++, here is the demonstration of the space issue you have (if you only use the addition operator):

#include <string>
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
    string str = "Welcome to the computer world.";
    string strWords[5];
    short counter = 0;
    for(short i=0;i<str.length();i++){
        strWords[counter] += str[i]; // Append fixed
        if(str[i] == ' '){
            counter++;
        }
    }
    for(short i=0;i<5;i++){
        cout << strWords[i] << "(" << strWords[i].size() << ")" << endl;
    }
    return 0;
}

Result:

Space at the end of each string

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71