在 C++ 中使用递归的反向数组
reverse array using recursion in c++
赋值相对简单,使用ReverseStringRecursive
函数反转数组main
。但是,限制是我只能使用单个int
和char
而不能使用其他声明变量的内容(这包括禁止for
循环等)。此外,我只能使用iostream
和conio.h
的额外库。我遇到的问题是,当我只需要向后打印时,string
将向前打印,然后向后打印。reverseMe
变量指向包含"abcdefghijklmnopqrstuvwxyz"
的main
中的string
。这个函数不是假设打印string
只是反转然后main
将打印字符串。
// INCLUES AND NAMESPACES
#include <iostream>
#include<conio.h>
using namespace std;
// CONSTANTS
const int STRING_SIZE = 100;
// PROTOTYPES
int ReverseStringRecursive(char*);
// MAIN
int main() {
// create a string
char someString[STRING_SIZE] = "abcdefghijklmnopqrstuvwxyz";
// display the string before being reversed
cout << "The string contains: " << endl;
cout << someString << endl << endl;
// make the call to the recursive function
cout << "CALL THE REVERSING FUNCTION" << endl << endl;
ReverseStringRecursive(someString);
// display the string after being reversed
cout << "The string contains: " << endl;
cout << someString << endl;
// exit program
_getch();
return 0;
}
int ReverseStringRecursive(char* reverseMe) {
// YOUR IMPLEMENTATION GOES HERE...
int position = 0;
char holder = ' ';
if (reverseMe[0] == ' ') {
return 1;
}
else {
holder = reverseMe[position];
}
ReverseStringRecursive(reverseMe + 1);
while (reverseMe[position] != ' ') {
position++;
}
reverseMe[position] = holder;
return position;
}
我得到的示例输出:
"abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedcba"
我应该得到什么:
"zyxwvutsrqponmlkjihgfedcba"
棘手的问题。 您必须缩短每次递归的内部字符串,方法是在调用递归函数之前在最后一个字符上放置"\0",然后在递归调用后执行交换。
算法:
0. save the index of the last character in the string
1. Save the last character of the current string
2. Set the last character of the current string to null (use the saved index)
3. Call the recursive function starting one character in which will recurse the algorithm for the next inner string (we have already shortened the end of the recursed string)
4. Once the recursion has finished, set the last character to the first char of the current string; then
5. set the first character of the current string to the saved character (which was at the end)
这也适用于奇数长度的字符串。
以下代码应该适用于窗口系统。 要使其在Linux上运行,只需注释掉conio.h包含行,注释__getch()行并取消注释cin.getch()行。
// INCLUES AND NAMESPACES
#include <iostream>
#include <conio.h>
using namespace std;
// CONSTANTS
const int STRING_SIZE = 100;
// PROTOTYPES
int ReverseStringRecursive(char *);
char *orig;
// MAIN
int main()
{
// create a string
char someString[STRING_SIZE] = "abcdefghijklmnopqrstuvwxyz";
orig = someString;
// display the string before being reversed
cout << "The string contains: " << endl;
cout << someString << endl << endl;
// make the call to the recursive function
cout << "CALL THE REVERSING FUNCTION" << endl << endl;
ReverseStringRecursive(someString);
// display the string after being reversed
cout << "The string contains: " << endl;
cout << someString << endl;
// exit program
_getch(); // uncoment conio.h on a windows system
// std::cin.get(); // use this if on a linux system
return 0;
}
int ReverseStringRecursive(char *reverseMe)
{
int last_index = 0;
while (reverseMe[last_index + 1] != ' ')
last_index++;
char save_char = reverseMe[last_index];
if (*reverseMe != ' ') {
reverseMe[last_index] = ' '; // shorten the inner string by one
// recurse on the shorter string
ReverseStringRecursive(reverseMe + 1);
// save the outer two characters
reverseMe[last_index] = *reverseMe;
*reverseMe = save_char;
}
}
您正在覆盖终止' '
,从而损坏您的字符串。当您的 while 循环存在时,reverseMe[position]
位于' '
处,然后用值holder
覆盖它。 您的字符串不再以 null 结尾,并且在下一个 while 循环中,当它访问数组边界之外时,您将在下一个 while 循环中获得未定义的行为。
相关文章:
- 使用递归的数组的最小值.这是怎么回事
- 数组元素打印的递归方法
- 使用递归模板动态分配的多维数组
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 这种用于查找连续子数组中最大和的递归算法有什么优势吗?
- 使用递归输出数组
- 使用递归反转和打印出数组的内容
- 递归二进制搜索与字符串数组
- 将数组传递给递归函数,同时C++修改它
- 使用递归的序列到达数组末尾的最小跳转次数
- 这是定义一组递归规则的正确方法吗?
- 斐波那契数的递归函数
- 尝试递归获取数组元素的总和
- 在C++中对二维数组进行归零.循环需要2个吗
- C++排序数组递归
- 当我的编译器不符合标准时,如何将数组成员归零
- 数独递归,接受零为合法
- 通过除数组递归地计算和
- 动态数组递归与不同的编译器
- 不使用循环和数和的数字根递归