Tuesday, 17 January 2012

Substring Replacement


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],str1[15],str2[15],temp[50];
char *ptr;
int cnt;
clrscr();
printf(“enter a line of text..... \n”);
gets(str);
printf(“enter the string to be replaced... \n”);
gets(str1);
printf(“enter the replacing string...);
gets(str2);
printf(“\n 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 

No comments:

Post a Comment