c++ - 如何反转字符串中单词(不是字母)的顺序?

标签 c++

<分区>

我将如何反转字符串中单词的顺序?我试过这个但它不起作用:

string sen = "Go over there";
reverse(sen.begin(), sen.end());

但这会颠倒整个字符串,但不会使单词保持正确的顺序。如何只反转字符串中单词的顺序?

最佳答案

我以前写过很多这样的字符串函数:

// Make copy of this original if you don't wish to destroy in the process
string sen    = "Go over there";

// string that will become your reversed string
string newStr = new string();

// A temp variable that will hold the current position of the last separator character
int aChar     = -1;

////////////////////////////////////////////////////////////////////////////////////////
// You may want to delete pre and post spaces here
////////////////////////////////////////////////////////////////////////////////////////

// Loop through the entire string until the original is empty
while(sen.length > 0){
    // Find the last separator character (in your case a space) within the string
    aChar   = sen.find_last_of(" ");

    // Append the word created from one char forward of the last found separator char
    // to the end of the CURRENT version of the original string
    newStr += sen.substr(aChar + 1, sen.length - aChar - 1);

    // Store a new version of the original string that is the SUBSTRING from beginning (char 0)
    // to one char before the last found separator character
    sen     = sen.substr(0, aChar - 1);

    // Need to add the space between the words, but only if the new substring is not empty
    if(sen.length > 0) newStr += " ";
}

我没有测试过这段代码,但如果 API 按照预期的方式工作,那么从算法上讲这应该可以工作。

作为 API,这可能如下所示

string reverse(string inStr){
    // Make copy of the original so we don't destroy it in the process
    string sen    = inStr.copy();

    // string that will become your reversed string
    string newStr();

    // A temp variable that will hold the current position of the last separator character
    int aChar     = -1;

    ////////////////////////////////////////////////////////////////////////////////////////
    // You may want to delete pre and post spaces here
    ////////////////////////////////////////////////////////////////////////////////////////

    // Loop through the entire string until the original is empty
    while(sen.length > 0){
        // Find the last separator character (in your case a space) within the string
        aChar   = sen.find_last_of(" ");

        // Append the word created from one char forward of the last found separator char
        // to the end of the CURRENT version of the original string
        newStr += sen.substr(aChar + 1, sen.length - aChar - 1);

        // Store a new version of the original string that is the SUBSTRING from beginning
        // (char 0) to one char before the last found separator character
        sen     = sen.substr(0, aChar - 1);

        // Need to add the space between the words, but only if the new substring is not empty
        if(sen.length > 0) newStr += " ";
    }

    return newStr;
}

int main(int argc, char *argv[]){
    string sen = "Go over there";
    string rev = reverse(sen);
}

关于c++ - 如何反转字符串中单词(不是字母)的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21618521/

相关文章:

c++ - 我该如何制作这个有效的 xml?

c++ - PCRE多行匹配问题

c++ - 使用 OpenSSL API 比较两个公钥

c++ - 为什么我的 DLL 文件和它对应的 LIB 文件有不同的名字?

c++ - 标题栏中的汉字

c++ - 如何捕获被阻止窗口的点击?

c++ - Qt Quick2窗口没有aero不能透明

C++构造函数混淆

c++ - 有没有一种优雅的方式来桥接 Asio 中的两个设备/流?

c++ - 非成员运算符重载应该放在哪里?