ProAnswers.org

Write a C program to perform the Substring Replacement ?

#include 

#include 

#include 

void main() 

{ 

char str[50],str1[15],str2[15],temp[50]; 

char *ptr; 

int cnt; 

clrscr(); 

printf(“enter a line of text..... 

”);

gets(str); 

printf(“enter the string to be replaced... 

”);

gets(str1); 

printf(“enter the replacing string...”); 

gets(str2); 

printf(“

the replaced line of the text…”);

while(1) 

{ 

ptr=strstr(str,str1); 

if(ptr==‟\o‟) 

break; 

cnt=ptr-str; 

strncpy(temp,str,cnt); 

temp[cnt]=‟10‟; 

strcat(temp,str+cnt+strlen(srt1)); 

strcpy(str1,temp); 

puts(str); 

} 

getch(); 

} 

Output: 

enter the line of text... i love india 

enter the string to be replaced..india 

enter the replacing string...my parents 

the replaced line of text 

i love my parents