/* * To replace all occurrences of a substring t in s with w,using pointers * @author-Karan Narain(karan@iitk.ac.in) * */ #include int replace_substring(char *s,char *t,char *w) { int i=0,index=-1,count=0; char *copy,*x,*y,*z; copy=s; while(*s!='\0') { if(*s==*t) { index=i; x=s; y=t; while(*x==*y && *x!='\0') { x++; y++; } //if t is present completely in s if(*y=='\0') { x=s; z=w; //replace t with w while(*z!='\0') { *x=*z; x++; z++; } count++; } } s++; i++; } printf("After replacement,string is %s\n",copy); return count; } int main() { int count; char A[80],B[80],C[80],*s=NULL,*t=NULL,*w=NULL; printf("Enter the string s\n"); scanf("%s",&A); printf("Enter the string t\n"); scanf("%s",&B); printf("Enter the string w\n"); scanf("%s",&C); s=A; t=B; w=C; count=replace_substring(s,t,w); printf("Replaced %d occurrences\n",count); return 0; }