FINDING AND REPLACING SUBSTRINGS

/*  FINDING AND REPLACING SUBSTRINGS  */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
  char s1[50],s2[50],s3[50],a[20],r[50];
  int i,j,k,l1,l2,s=0,p,c=0;
  clrscr();
  printf("\n\t\tINPUT:");
  printf("\n\t\t------");
  printf("\n\t\tEnter the string:  ");
  gets(s1);
  printf("\n\t\tEnter the substring:  ");
  gets(s2);
  printf("\n\t\tEnter the replace string:  ");
  gets(s3);
  l1=strlen(s1);
  l2=strlen(s2);
  for(i=0;i<l1;i++)
    {
      k=0;
      if(s1[i]==s2[0])
       {
         for(j=i;j<i+l2;j++)
          {
            a[k]=s1[j];
            k++;
          }
         a[k]='\0';
         if(strcmp(a,s2)==0)
           {
             for(p=0;s3[p]!='\0';p++)
              {
                r[s++]=s3[p];
              }
             i=i+l2-1;
             c++;
           }
         else
            r[s++]=s1[i];
       }
      else
         r[s++]=s1[i];
     }
  r[s++]='\0';
  printf("\n\t\tOUTPUT:");
  printf("\n\t\t-------");
  if(c==0)
    {
      printf("\n\t\tThe Substring is not Found");
    }
  else
    {
      printf("\n\t\tThe Substring is Found");
      printf("\n\t\tReplaced string is :  %s",r);
    }
  getch();
}


INPUT:
------
Enter the string:  I am doing BCA
Enter the substring:  BCA
Enter the replace string:  Bachelor Degree

OUTPUT:
-------
The Substring is Found
Replaced string is :  I am doing Bachelor Degree


INPUT:
------
Enter the string:  I am doing BCA
Enter the substring:  cs
Enter the replace string:  bca

OUTPUT:
-------
The Substring is not Found



No comments:

Related Posts with Thumbnails