以下 c 编程代码给出了核心转储分段错误,请告诉我为什么我会收到此错误,并通过提供此代码的更正版本来帮助我。谢谢!
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, Flag;
Flag = 0;
printf("\n Please Enter any String : ");
fgets(str,sizeof(str),stdin);
printf("%s",str);
printf("\n Please Enter the Character that you want to replace with : ");
scanf("%c", &ch);
for(i = 0; i <= strlen(str); i++)
{
str[i]=ch;
}
str[i]='\0';
printf("\n The characters have been found and replaced with %c and they occur %d times ", ch, i + 1);
printf("The replaced string is %s ",str);
return 0;
}
尝试这个。
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, Flag;
Flag = 0;
printf("\n Please Enter any String : ");
fgets(str,sizeof(str),stdin);
printf("%s",str);
printf("\n Please Enter the Character that you want to replace with : ");
scanf("%c", &ch);
for(i = 0; i < strlen(str); i++) //change <= to <
{
str[i]=ch;
}
str[i]='\0';
printf("\n The characters have been found and replaced with %c and they occur %d times ", ch, i + 1);
//printf("The replaced string is %s ",str);
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句