简体   繁体   中英

char * array manipulation on recursive function

I have a program that calculates the edit distance of two strings. it also outputs all the edit operations to obtain the complete transformation. i wrote a recursive function that explores the matrix filled by the edit distance calculation function and reconstructs the path

void reconstruct_path(char *s1, char *s2 ,int i, int j , matrix_t matrix)

 void reconstruct_path(char *s1, char *s2 int i, int j , matrix_t matrix)
 {
    if(matrix[i][j].parent == -1) return;


    if (matrix[i][j].parent == MATCH) 
    {
    reconstruct_path(s1,s2,i-1,j-1,matrix);
    match_out(s1, s2 , i, j);
    return;
    }

    if (matrix[i][j].parent == INSERT) 
    {
    reconstruct_path(s1,s2,i,j-1,matrix);
    insert_out(s2, j);
    return;
    }

    if (matrix[i][j].parent == DELETE) 
    {
    reconstruct_path(s1,s2,edit,i-1,j,matrix);
    delete_out(s1, i);
    return;
    }

}`

as you can notice there are three functions that this function calls

- void match_out(char *s1, char *s2,int i, int j)
- void insert_out(char *t, int j)
- void delete_out(char *s, int i)

void match_out(char *s1, char *s2,int i, int j)

void match_out(char *s1, char *s2 ,int i, int j)
{
    if (s1[i] == s2[j]) 
    {
        printf("M no edit needed \n" );
    }
    else 
    {
        printf("S subst %c with %c \n",s1[i] , s2[j]);
    }
}

void insert_out(char *t, int j)

void insert_out(char *t, int j)
{
    printf("I Insert %c\n",t[j]);
}

void delete_out(char *s, int i)

void delete_out(char *s, int i)
{
    printf("D delete %c\n",s[i]);
}

this produces an output like this

from "parent" to "cousin" :

S subst p with c
S subst a with o
S subst r with u
S subst e with s
S subst n with i
S subst t with n

i want to improve this to obtain a more precise output like this:

from "parent" to "cousin" :

S subst p with c parent -> carent
S subst a with o carent -> corent
S subst r with u corent -> couent
S subst e with s couent -> cousnt
S subst n with i cousnt -> cousit
S subst t with n cousit -> cousin

Do you ave any suggestion? (i'm noto so good with C string manipulation)


[update from comments to this answer :]

What is the data type of two strings which are recieved in s1 and s2 ? (asked by vj1207 )

They are declared in main() like this char *str_a = " parent"; char *str_b = " cousin"; char *str_a = " parent"; char *str_b = " cousin";

You can add few line in match_out

void match_out(char *s1, char *s2, char **edit ,int i, int j)
{
    if (s1[i] == s2[j]) 
    {
        printf("M no edit needed \n" );
    }
    else 
    {
        printf("S subst %c with %c ",s1[i] , s2[j]);
        //**from here**
        printf("%s -> ",s1);
        s1[i]=s2[j];
        printf("%s\n",s1);
        //upto here
    }
}

Update

you can declare the char array as

char str[]= {'p','a','r','e','n','t'};

if you declare it as

char * str = "parent";

then you can't modify it. And that is why you were getting the mentioned error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM