Thursday, November 22, 2012

C PROGRAM FOR REPLACING STRING


/* C PROGRAM FOR  REPLACING STRING*/

#include<stdio.h>
#include<conio.h>
#include<string.h>

 void main()
 {
          int slen,sublen,newlen,reslen,m,k,i,j,temp;
          char str[40],substr[20],newstr[20],resstr[25];
          clrscr();
          printf("\n\t\t STRING REPLACING");
          printf("\n\t\t ~~~~~~~~~~~~~~~~~");
          printf("\n Enter any string:");
          gets(str);
          slen=strlen(str);
          printf("\n Enter the substring:");
          gets(substr);
          sublen=strlen(substr);
          printf("\n Enter the replacement string:");
          gets(resstr);
          reslen=strlen(resstr);
          j=0;
          k=0;
for(i=0;i<slen;i++)
{
         newstr[k]=str[i];
         k++;
         if(str[i]==substr[j])
         j++;
else
         j=0;
if(j==sublen)
{
         k=k-sublen;
         j=0;
for(m=0;m<reslen;m++)
{
         newstr[k]=resstr[m];
         k++;
     }
   }
}
      newstr[k]='\0';
      printf("\n The replaced string is %s",newstr);
      getch();
}


OUTPUT:

                 STRING REPLACING
                 ~~~~~~~~~~~~~~~~~
 Enter any string: RAJ

 Enter the substring: R

 Enter the replacement string: T

 The replaced string is: TAJ





0 comments:

Post a Comment