Sunday, 7 April 2013

Serach and replace a string in c program

Program to search and replace

 
# include< stdio.h >
#include< conio.h >
void main()
{
char str[100],s[100],r[100],ans[100];
int i,j,c,m,k;
clrscr();
printf("\nEnter a string \n");
gets(str);
printf("\nEnter a search string \n");
flushall();
gets(s);
printf("\nEnter a replace string \n");
flushall();
gets(r);
i = m = c = j = 0;
while ( str[c] != '\0')
{
if ( str[m] == s[i] )
{
i++;
m++;
if ( s[i] == '\0')
{
for(k=0; r[k] != '\0';k++,j++)
ans[j] = r[k];
i=0;
c=m;
}
}
else
{
ans[j] = str[c];
j++;
c++;
m = c;
i=0;
}
}
ans[j] = '\0';
printf("\nThe resultant string is\n%s" ,ans);
}


 

No comments:

Post a Comment