我正在尝试使用静态变量构建一个递归函数,该函数将用星号字符替换空格,但是有些东西不对劲。我收到错误 Runtime error time: 0 memory: 3472 signal:11
。
代码
#include <iostream>
using namespace std;
string replace(string s) {
static int n = 0;
if (n == s.length()) return s;
if (s[n] == ' ') s[n] = '*';
n++;
replace(s);
}
int main() {
string s= "ssdfa sadfs";
cout<<replace(s);
return 0;
}
最佳答案
您只是从递归的基本情况返回一个字符串,而不是所有其他情况。将最后一行更改为:
return replace(s);
关于c++ - 递归中的运行时内存错误用c++中的字符函数替换空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26558820/