#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #include <memory.h> char *replace(char *cp1, char *cp2, char *cp3, char *cp4){ char* pos; while(1){ if ((pos = (char*)strstr(cp1, cp2)) != NULL){ strncat(cp4, cp1, pos-cp1); strcat(cp4, cp3); cp1=pos; cp1+=strlen(cp2); } else { strcat(cp4, cp1); break; } } return(cp4); } void main(){ char *a = "蛎aa誼bb慶cc括窮憩ee", *x, *z; int num; num = strlen(a) * 2 + 1; x=(char*)malloc(num); z=(char*)malloc(num); memset(x, 0x00, num); memset(z, 0x00, num); printf("a =[%s]\n", a); replace(a, "", "(株)", x); printf("replace1=[%s]\n", x); replace(x, "", "(有)", z); printf("replace2=[%s]\n", z); memset(x, 0x00, num); replace(z, "", "(代)", x); printf("replace3=[%s]\n", x); memset(z, 0x00, num); free(z); free(x); } |