REPLACING PATTERN

 REFERRED FROM : GURUPRASAD M S

#include<stdio.h>
char str[100],pat[100],rep[100],ans[100];
int c=0,m=0,i=0,j=0,k=0,flag=0;
void stringmatch();
void main()
{
printf("Enter the Main string:\n");
gets(str);
printf("Enter the Pattern string\n");
gets(pat);
printf("Enter the Replace string:\n");
gets(rep);
stringmatch();
if(flag==1)
{
printf("Resultant string is %s\n",ans);
}
else
{
printf("Pattern not found\n");
}
}
void stringmatch()
{
while(str[c]!='\0')
{
if(str[m]==pat[i])
{
i++;
m++;
if(pat[i]=='\0')
{
flag=1;
for(k=0;rep[k]!='\0';k++)
{
ans[j]=rep[k];
j++;
}
c=m;
i=0;
}
}
else
{
ans[j]=str[c];
j++;
c++;
m=c;
i=0;
}
}
ans[j]='\0';
}

OUTPUT:








REFERRED FROM : PRITHVIRAJ JAIN





#include<stdio.h>
#include<string.h>
void read();
void pattern();
char str[30],pat[30],rep[30],ans[30];
int i,j,c,k,m,flag=0;
void main()
{
    read();
    pattern();
}
void read()
{
    printf("ENTER THE STRING\n");
    gets(str);
    printf("ENTER THE PATTERN\n");
    gets(pat);
    printf("ENTER THE REPLACE STRING\n");
    gets(rep);
}
void pattern()
{
    i=m=c=j=0;
    while(str[c]!='\0')
    {
        if(str[m]==pat[i])
        {
            i++;
            m++;
            if(pat[i]=='\0')
            {
                flag=1;
                for(k=0;rep[k]!='\0';k++,j++)
                ans[j]=rep[k];
                i=0;
                c=m;
            }
        }
        else
        {
            ans[j]=str[c];
            j++;
            c++;
            m=c;
            i=0;
        }
    }
if(flag==0)
{
printf("PATTERN NOT FOUND IN A GIVEN STRING\n");
}
else
{
    ans[j]='\0';
    printf("THE RESULTANT STRING IS %s\n",ans);
}
}

OUTPUT: