#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void ReplaceStr(char *source, char *aa, char *bb) // aa-->bb,aa被bb替换。
{
int len=64,StringLen;
char temp[len];
char *FindPos = strstr(source, aa);
if( (!FindPos) || (!aa) )
return -1;
while( FindPos )
{
memset(temp, 0, sizeof(temp));
StringLen = FindPos - source;
strncpy(temp, source, StringLen);
strcat(temp, bb);
strcat(temp, FindPos + strlen(aa));
strcpy(source, temp);
FindPos = strstr(source, aa);
}
}
int main()
{
int i,j;
char a[]="abc\"de",*pa=a;//源字符串
char b[]="ab",*pb=b; //替换前的 字符串
char c[]="123",*pc=c; //替换后的 字符串
printf("before: pa=%s \n",pa);
ReplaceStr(pa,pb,pc);
printf("after: pa=%s \n",pa);
// printf("pb=%s \n",pb);
// printf("pc=%s \n",pc);
return 0;
}
#include <string.h>
#include <stdlib.h>
void ReplaceStr(char *source, char *aa, char *bb) // aa-->bb,aa被bb替换。
{
int len=64,StringLen;
char temp[len];
char *FindPos = strstr(source, aa);
if( (!FindPos) || (!aa) )
return -1;
while( FindPos )
{
memset(temp, 0, sizeof(temp));
StringLen = FindPos - source;
strncpy(temp, source, StringLen);
strcat(temp, bb);
strcat(temp, FindPos + strlen(aa));
strcpy(source, temp);
FindPos = strstr(source, aa);
}
}
int main()
{
int i,j;
char a[]="abc\"de",*pa=a;//源字符串
char b[]="ab",*pb=b; //替换前的 字符串
char c[]="123",*pc=c; //替换后的 字符串
printf("before: pa=%s \n",pa);
ReplaceStr(pa,pb,pc);
printf("after: pa=%s \n",pa);
// printf("pb=%s \n",pb);
// printf("pc=%s \n",pc);
return 0;
}