#include<stdio.h> #include<string.h> int input_array(char *ptr) { scanf("%s",ptr); getchar(); } int str_replace(char* ptra,char *ptrb,char *ptrc) { int reta = 0,retb = 0; int i = 0,j= 0,x = 0; char* pa = NULL; char* pb = NULL; reta = strlen(ptra); retb = strlen(ptrb); for(i=0;i<reta;i++) { if(*ptra==*ptrb) { pa=ptra; pb=ptrb; for(j=1; j<retb; j++) { if(( *(pa+j) == *(pb+j))&&(j<=(retb-1))) { for(x =0;x<retb;x++) { *(ptra+x)=*(ptrc+x); } } else break; } } ptra++; } } int main(int argc,char *argv[]) { int i = 0; char a[100] = {0}; char b[100] = {0}; char c[100] = {0}; printf("请输入一串字符串"); input_array(a); printf("请输入要替换的字符串"); input_array(b); printf("请输入目标字符串:"); input_array(c); str_replace(a,b,c); for(i=0;i<strlen(a);i++) { printf("%c",a[i]); } printf("\n"); return 0; }
时间: 2024-10-10 17:49:58