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

C++ 如何通过递归反转字符串中单词的顺序,c++,string,recursion,C++,String,Recursion,我想用这个词来颠倒我的字符串顺序。比如,如果字符串是“Cat is running”,那么它应该是“running is Cat”。 代码如下: #include<iostream> #include<string> using namespace std; void reverseString(string str); int length, lastLength; int main() { string s; cout << "Ent

我想用这个词来颠倒我的字符串顺序。比如,如果字符串是“Cat is running”,那么它应该是“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);
    }
}
#包括
#包括
使用名称空间std;
无效反向限制(字符串str);
int-length,lastLength;
int main(){
字符串s;

您可能有两个不同的错误。
.at
是一个方法,因此它应该被称为
.at()
而不是
。at[]
。其次,您将
char
string
(“”)进行比较。因此,您应该将“”替换为“”

#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;
无效反向限制(字符串str);
int-length,lastLength;
int main(){
字符串s;

cout
std::string
有许多辅助函数,例如
string::find
string::rfind
、和
std::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());
}
void reverseString(std::string str,size\u t end)
{
尺寸位置=str.rfind('',结束);
if(pos==std::string::npos)
{

CUT< P>这里是一个试图保存解决方案中的逻辑的版本,只有一点C++代码> <代码>便利:

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);
}
void output\u reverse\u string(string str、int last、int current){
/*终止条件:我们已耗尽输入:*/
如果(当前==0){

std::cout这是编译错误还是运行时错误或输出错误,请澄清!!我看不到在main中调用了
reversesetring
。我会避免在递归中使用全局变量,它们使代码很难读取/分析。您的循环不变量及其主体不正确。请查看您希望用它实现的功能。我得到了错误。它是严重不成熟。喜欢你的方法:)