c语言字符串替换,例如将abcdabac中的ab替换为xyz
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char str1[1000] = { 0 }, str2[100] = { 0 }, str3[100] = { 0 };//字符串1串长最大数值为999字节,字符串2、3串长最大数值为99
scanf("%s%s%s", str1,str2,str3);
char temp[100] = { 0 }, temp1[1000] = { 0 }, temp2[1000] = { 0 };
int i = 0, j = 0;
for (i = 0; i < strlen(str1) - strlen(str2); i++)
{
int len = i + strlen(str2);
for (j = i; j < len; j++)
{
temp[j - i] = str1[j];
}
if (strcmp(temp, str2) == 0)
{
for (j = 0; j < i; j++)
temp1[j] = str1[j];
for (j = len; j < strlen(str1); j++)
temp2[j - len] = str1[j];
strcpy(temp, str3);
strcpy(str1, temp1);
strcat(str1, temp);
strcat(str1, temp2);
}
}
printf("%s", str1);
system("pause");
return 0;
}