gpt4 book ai didi

c++ - 为什么最后是垃圾字符?

转载 作者:行者123 更新时间:2023-11-30 19:54:41 25 4
gpt4 key购买 nike

示例如下:

   main()
{
// code to insert characters in between where letters repeat twice
char b[20];
int l,i=0;

cin>>b;
l=strlen(b);
while(i<=l-1)
{ if (b[i]==b[i+1]){
for(int j=l-1;j>i;j--)
b[j+1]=b[j];
b[i+1]='x';
l++;
}
i++;
}
puts(b);
getch();
return 0;
}

最佳答案

b[i+1]='x'中,\0字符被覆盖。当您使用 puts 打印 b 时,它会调用未定义的行为。

关于c++ - 为什么最后是垃圾字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20805280/

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