如何通过递归来反转字符串中单词的顺序

时间:2016-05-09 04:07:23

标签: c++ string recursion

我希望这可以通过单词来反转我的字符串顺序。就像一个字符串是“Cat正在运行”,那么它应该是“正在运行的猫”。 这是代码:

#include<iostream>
#include<string>

using namespace std;
void reverseString(string str);
int length, lastLength;

int main() {

    string s;
    cout << "Enter a string to reverse its words: ";
    getline(cin, s);
    lastLength = s.length() - 1;
    length = lastLength;
    cout << "\nThe string in reverse order is ";
    cout << endl;
}

void reverseString(string str) {

    if (length < 0)
        return;
    else {
        if (str.at[length] == " " || length == 0)
        {
            if (length == 0)
                length = -1;
            for (int i = length + 1; i < lastLength; i++)
                cout << str.at[length];
            lastLength = length - 1;
        }
        length--;
        reverseString(str);
    }
}

它显示指针和数组的一些错误。我不知道如何解决这个问题。 任何帮助将真正感激! :)

3 个答案:

答案 0 :(得分:0)

您有两种不同的错误。 .at是一种方法,因此应将其称为.at()而不是.at[]。其次,您将charstring(“”)进行比较。所以,你应该用''替换“”。

#include<iostream>
#include<string>

using namespace std;
void reverseString(string str);
int length, lastLength;

int main() {

    string s;
    cout << "Enter a string to reverse its words: ";
    getline(cin, s);
    lastLength = s.length() - 1;
    length = lastLength;
    cout << "\nThe string in reverse order is ";
    cout << endl;
}

void reverseString(string str) {

    if (length < 0)
        return;
    else {
        if (str.at(length) == ' ' || length == 0) // <- note the changes here
        {
            if (length == 0)
                length = -1;
            for (int i = length + 1; i < lastLength; i++)
                cout << str.at(length); // <- note the changes here
            lastLength = length - 1;
        }
        length--;
        reverseString(str);
    }
}

我没有检查逻辑。您可以继续处理逻辑:)

答案 1 :(得分:0)

std::string有许多辅助函数,例如string::findstring::rfindstd::substr,您可以使用它们来操作字符串,而不是单独访问字符。例如:

void reverseString(std::string str, size_t end)
{
    size_t pos = str.rfind(' ', end);
    if (pos == std::string::npos)
    {
        cout << str.substr(0, end + 1) << endl;
    }
    else
    {
        cout << str.substr(pos + 1, end - pos) << endl;
        reverseString(str, pos - 1);
    }
}

int main()
{
    std::string s = "Enter a string to reverse its words";
    cout << s << endl;
    reverseString(s, s.length());
}

答案 2 :(得分:0)

这是一个试图保留解决方案逻辑的版本,只有一点点C ++ <string>方便:

void output_reverse_string(string str, int last, int current) {
    /* Terminating condition: we've exhausted the input: */
    if (current == 0) {
        std::cout << str.substr(current, 1 + last - current);
        return;
    }
    /* Recurse until we've found a space: */
    if (str.at(current) != ' ') {
        output_reverse_string(str, last, current - 1);
        return;
    }
    /* Since we've found a space, output the following word: */
    std::cout << str.substr(current + 1, last - current);
    /* Just for readability, can be skipped: */
    std::cout << " ";

    /* Recurse on the *remaining* string contents: */
    output_reverse_string(str, current - 1, current - 1);
}