This a program for replacing all substring with a given string in a string..but it is showing segmentation fault..pls help..
char s[1000]="i am a good boy,he is a good boy";
char rep[100]="good";
char new[100]="bad";
char result[1000];
char *tmp;
int len;
char *k=s;
while(1)
{
tmp=strstr(k,rep);
if(tmp==NULL)
break;
len=tmp-k;
tmp=tmp+strlen(rep);
strncpy(result,k,len);
strcat(result,new);
k=k+len+strlen(rep);
}
strcat(result,tmp);
puts(result);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…