Program To Find and replace the given string from the main string by kalyaniswami00

VIEWS: 8 PAGES: 3

More Info
									Program To Find and replace the given string from the main string

#include<stdio.h>

#include<string.h>

void findreplace(char *string, char *find, char *replace);

void main()

{

        char string[30],find[30],replace[30];

        printf("\nEnter string ::");

        flushall();

        gets(string);

        printf("\nEnter string to be find::");

        flushall();

        gets(find);

        printf("\nEnter string to be replace::");

        flushall();

        gets(replace);

        findreplace(string,find,replace);

        getch();

}

void findreplace(char *string, char *find, char *replace)

{

        char str1[30],str2[30];

        int i,j,k,l1,l2,pos;

        int xstrlen(char s[]);

        l1=xstrlen(string);
l2=xstrlen(find);



for(i=0;i<=l1-l2;i++)

{

         j=0;

         k=i;

         while(string[k]==find[j] && (j<l2))

         {

                     k++;

                     j++;

         }

         if(j==l2)

                     break;

}

pos=i;

if(pos>=0)

{

         printf("\n%s found at %d to %d\n\n",find,pos+1,pos+l2);



         for(i=0;i<pos;i++)

         {

                     str1[i]=string[i];

         }

         str1[i]='\0';
                 j=0;

                 for(i=pos+l2;i<l1;i++)

                 {

                          str2[j]=string[i];

                          j++;

                 }

                 str2[j]='\0';

                 strcat(str1,replace);

                 strcat(str1,str2);

                 printf("\nResultant string is \n \n%s\n\n",str1);

        }

        else

        {

                 printf("\nString %s not found\n",find);

        }

}

int xstrlen(char s[])

{

        int i;

        for(i=0;s[i]!='\0';i++)

        {}

        return i;

}

								
To top