Friday 28 March 2014

String Search and Replacement function in C Language

/* A case-sensitive string replacement program
The Basic principle behind this program depends upon three cases
Case 1: If replacement string equals to 'find' string in length
    then no. more space is required so character are simply replaced
Case 2:    If replacement string is less than 'find' string in length
    then some is created after replacing the characters
    so another loop is required for removing that holes
Case 3:    If replacement string is greater than 'find' string in length
    then more space is required to be created between characters for
    replacement string, after that replacement is done.
*/

//Note: This program can be modified by using list and stack
//    to make it more efficient, robust and  flexible.
#include<stdio.h>
#include<conio.h>
#include<string.h>

char* replace(char[],char[],char[]);

void main ()
{
    char str[50]="Look Hook Nook";
    clrscr();
    printf("Before: %s",str);
    printf("\nAfter: %s", replace(str,"k","am"));
    printf("\nBefore: %s",str);
    printf("\nAfter: %s", replace(str,"Loo","S"));
    printf("\nBefore: %s",str);
    printf("\nAfter: %s", replace(str,"Hoo","Dre"));
    printf("\nBefore: %s",str);
    printf("\nAfter: %s", replace(str,"ooam","ame"));
    getch();

}

// This function can work with max. of 100 occurence of 'to find' string
// due to static declaration of memory limit.
char* replace(char str[],char find[],char rep[]){
    int occ[100],i,j,loc,c=0,next,start;
    int lens,lenf,lenr;
    int diff,noOfBytes;
    lens=strlen(str);
    lenf=strlen(find);
    lenr=strlen(rep);
    //find the no. of occurence of 'find' string
    for(i=0;i<lens;i++) {
        loc=i;
        for(j=0;j<lenf;j++){
            if(str[i+j]!=find[j]){
                loc=-1;
                break;
            }
        }
        if(loc!=-1){
            occ[c]=loc;
            c++;
        }

    }
    //check difference between length of 'find' and 'replace' strings
    diff=lenr-lenf;
    //Case 1: When length of both strings is equal
    if(diff==0){
        for(i=0;i<c;i++){
            for(j=0;j<lenr;j++){
                str[occ[i]+j]=rep[j];
            }
        }
    }
    //Case 2: When length of replace string < length of find string
    else if(diff<0){
        //replace characters
        for(i=0;i<c;i++){
            for(j=0;j<lenr;j++){
                str[occ[i]+j]=rep[j];
            }
        }
        //fill holes
        for(i=0;i<c;i++){
            next=lens+diff*c;
            if(i<c-1) next=occ[i+1]-1;
            for(j=occ[i]+lenr-i;j<=next;j++){
                str[j]=str[j-diff*(i+1)];
            }
        }
        //append null character at end of string
        for(i=lens-1;i>=lens+diff*c;i--){
            str[i]='\0';
        }
    }
    //Case 3: When length of replace string > length of find string
    else if(diff>0){
        //make space
        next=lens-1;
        for(i=c-1;i>=0;i--){
            for(j=next;j>=occ[i];j--){
                str[j+diff*c-c+i+1]=str[j];
            }
            next=occ[i];
        }
        //replace characters
        for(i=0;i<c;i++){
            for(j=0;j<lenr;j++){
                str[occ[i]+j+i]=rep[j];
            }
        }

    }
    return str;                 //return result
}    //End of function
//END OF PROGRAM

No comments:

Post a Comment