gpt4 book ai didi

c++ - 递归中的运行时内存错误用c++中的字符函数替换空格

转载 作者:行者123 更新时间:2023-11-28 00:22:53 24 4
gpt4 key购买 nike

我正在尝试使用静态变量构建一个递归函数,该函数将用星号字符替换空格,但是有些东西不对劲。我收到错误 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com