Friday, September 21, 2012

program to perform string replace

c program to perform string replace..
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char cpy[30][30],st[30],t[30],a[20],b[20];
int l,i,j,c=0,r=0;
clrscr();
printf("Enter the string\n");
gets(st);
printf("Enter the search string\n");
scanf("%s",a);
printf("Enter the replace string\n");
scanf("%s",b);
l=strlen(st);
st[l]=' ';
for(i=0;i<=l;i++)
{
  if(st[i]!=' ')
  {
  t[r]=st[i];
  r++;
  }
  else
  {
  t[r]='\0';
  r=0;
  strcpy(cpy[c],t);
   c++;
  }
}
strcpy(st," ");
for(i=0;i<c;i++)
 {
if(strcmp(cpy[i],a)==0)
  {
  strcpy(cpy[i],b);
  }

 }
 for(i=0;i<c;i++)
 {
 strcat(st,cpy[i]);
 strcat(st," ");
 }
 printf("\n");
 puts(st);  //prints replaced string

getch();
}

No comments:

Post a Comment