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

How to reverse the order of words in a string through recursion

本文关键字:单词 顺序 字符串 何通过 递归      更新时间:2023-10-16

我希望这能按单词颠倒我的字符串顺序。就像如果一个字符串是"Cat正在运行",那么它应该是"running is 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);
    }
}

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

您有两个不同的错误。.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);
    }
}

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

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());
}

这里有一个版本,它试图在您的解决方案中保留逻辑,只需要一点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);
}