C++ 拆分一个句子,以便将每个单词添加到数组项中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26424871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:35:57  来源:igfitidea点击:

split a sentence so that adds each word in an array item

c++arrayssplit

提问by Lion King

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

回答by Marco A.

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

无论如何,我建议使用重复链接在 C++ 中拆分字符串?

回答by Gunther Van Butsele

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

非常丑陋的方法,@Cyber​​ 已链接到最佳答案。但这是您的“更正”版本:

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

回答by Rudolfs Bundulis

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:

正如评论中提到的,有很多更方便的方法来拆分字符串(strtok,std 功能等),但是如果我们谈论您的示例,您不应该分配 'str[i]' 而是附加它,因为它是一个单个字符,您希望像这样附加到当前单词中:

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 strWordsoutside bounds if you have more than five words. Consider using a following code:

但这仅适用于给定的输入数据,因为strWords如果您有五个以上的单词,您可以在边界之外访问数组。考虑使用以下代码:

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):

由于我假设您是 C++ 的新手,这里是您遇到的空间问题的演示(如果您只使用加法运算符):

#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

每个字符串末尾的空格