编写一个在字符串str中所有出现子串substr1的后面插入子串substr2

2024-12-04 15:30:37
推荐回答(1个)
回答1:

//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
#include "string.h"
void fun(char *str,char *substr1,char *substr2,int *pln){
char tmp[256],*p;
if(p=strstr(str,substr1))
strcat(strcpy(p,substr2),strcpy(tmp,p+=strlen(substr1)));
*pln=strlen(str);
}
void main(void){
char str[512],substr1[80],substr2[80];
int ln;
printf("Type str(length<256)...\nstr=");
gets(str);
printf("Type substr1(length<80)...\nsubstr1=");
gets(substr1);
printf("Type substr2(length<80)...\nsubstr2=");
gets(substr2);
fun(str,substr1,substr2,&ln);
printf("%s\n%d\n",str,ln);
}