0

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";

2
  • 2
    Your code does not compile. You declare match_out with 5 input arguments, but use it with 4. You declare reconstruct_path with 5 input arguments, but use it with 6. Commented Sep 22, 2014 at 11:01
  • you're right. It was a copy paste error :) Commented Sep 22, 2014 at 11:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

check that j and i are not negative. also why do you pass matrix by value? you don't seem to modify it. is it a 2D array?
1)There are no negative values. there is only one -1 that is strategically placed on matrix[0][0] as placeholder for the root element.2)yes it is a 2D array 3) It's not by value matrix_t is defined as typedef struct m_bucket ** matrix_t;
what is the data type of two strings which are recieved in s1 and s2 ?
They are declared in main() like this char *str_a = " parent"; char *str_b = " cousin";.
Then that is the problem. when you declare it as char *str_a = " parent"; then you can't modify it because it will be literal. You can not modify literal. I am updating the correct way of declaration in my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.