My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program to replace the substring with another substring of same or different length

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{  clrscr();
   char s1[500],s2[50],s3[50];
   int i,j,c=0,l1,l2,l3;
   cout<<"enter the string: \n";
   gets(s1);
   cout<<"enter the substring which would b replaced from substring: \n";
   gets(s2);
   cout<<"enter the string which will b replaced wid that substring: \n";
   gets(s3);
   l1=strlen(s1);
   l2=strlen(s2);
   l3=strlen(s3);
   for(i=0;i<l1;i++)
   {
      for(j=0;j<l2;j++)
      {
     if(s1[i+j]!=s2[j])
     break;
      }


      if(j==l2)
      {  int k=l1;
     while(k>=i)
     {  s1[l1+l3-l2]=s1[l1];
        l1--;
        k--;
      }

      k=0;
      while(s3[k]!='\0')
      {  s1[i]=s3[k];
         k++;i++;
      }

      l1=strlen(s1);
       }
    }
    cout<<"now the string become: \n"<<s1;
    getch();
    }

0 comments: