如何通过递归反转字符串中单词的顺序
How to reverse the order of words in a string through recursion
我希望这能按单词颠倒我的字符串顺序。就像如果一个字符串是"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[]
。其次,比较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::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());
}
这里有一个版本,它试图在您的解决方案中保留逻辑,只需要一点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);
}
相关文章:
- 文本文件中的单词链表
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 尝试在 c++ 中按字母顺序排列单词时出现分段错误
- 从单词c 的顺序消除元音
- 句子中的单词顺序相反
- 基于广告顺序重复次数最多的单词
- 字符串中单词的反向顺序
- 您如何扭转字符串中单词(而不是字母)的顺序
- 如何通过递归反转字符串中单词的顺序
- 按字母顺序排列单词
- 字符串中单词的相反顺序
- C++程序(按字母顺序打印出n个单词)运行时错误
- 通过递归以相反的顺序打印出一行中的单词
- 反转字符数组中单词的顺序
- 为什么我的ifstream程序以错误的顺序读取单词
- 反转句子中单词的顺序
- 从输入中读取单词序列。使用"Quit"终止,按输入顺序打印,不要打印两次单词。
- 将单词转换为字母的字母顺序值
- c++:我试图颠倒字符串中单词的顺序(而不是整个字符串)
- c++查找首尾字母相同的单词,并按字母顺序对它们进行排序