Tuesday, May 01, 2012

C Program That Replace The Searched Word With Another Word.

Hello friends in this program i enter a string and after that search a word in that string, if word found than replace that word with new word,but there is one condition ie.length of new word and searched word should be same.



#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 char str[100],ser[100],rep[100];
 int i,j,k,l,g=0,f=0,h,m;
 clrscr();
 printf("\n Enter the string:");
 gets(str);
 m=strlen(str);
 printf("\n Enter the word which you want to search:");
 gets(ser);
 l=strlen(ser);
 printf("\n Enter the word which you want to replace(length is equal to searched word):");
 gets(rep);
 for(i=0;str[i]!='\0';i++)
 {
  j=0;
  k=i;
  while(ser[j]!='\0')
  {
   if(str[k]==ser[j])
   {
    g++;
    if(g==l)
     f=1;
    h=i;
   }
   else if(str[k]!=ser[j])
    break;
   j++;
   k++;
  }
 }
 if(f==1)
 {
  printf("\n word is found now we can replace is:\n");
  j=0;
  for(i=0;i<m;i++)
  {
   if(i<h)
    printf("%c",str[i]);
   else if(i>=h&&i<=(h+l))
   {
    str[i]=rep[j];
    printf("%c",str[i]);
    j++;
   }
   else if(i>(h+l))
    printf("%c",str[i]);
  }
 }
 else if(f==0)
 {
  printf("\n word is not found you cant replace it:");
 }
 getch();
 return(0);

}


           Thanks Friends.........

No comments:

Post a Comment

Please Give Me Your Views

Popular Posts