Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi, I want to compare two string and find out how much this two strings are similar.
strcmp is not a good idea I think.
for example we have 3 strings. I need to find out similarity of string 1 and 2 is higher or similarity of string 1 and 3. Any Idea?

I want this for comparing two packet's payload.

thanks

Do we have such compare function in c?

What I have tried:

string str1 = "I am string number one"
string str2 = "I am string number two"
string str3 = "this is string number 3"
int similarity12,similarity13;
similarity12 = compare(str1,str2);
similarity13 = compare(str1,str3);
Posted
Updated 22-Apr-17 3:04am
v2

1 solution

It sounds like you might want Levenshtein distance - Wikipedia[^]
 
Share this answer
 
Comments
Member 11112168 22-Apr-17 9:25am    
Can we use levenshtein for assessing packet payload similarity?
PIEBALDconsult 22-Apr-17 10:28am    
It's just bytes. :)
Try it and see.
Member 11112168 23-Apr-17 1:32am    
In the code when I want to print the result I get "illegal write, out of bounds error"

#define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
static void levenshtein(char *s1, char *s2) {
      int x, y, s1len, s2len,similarity;
    s1len = strlen(s1);
    s2len = strlen(s2);
	printf("string1= %s \nstring2= %s \n",s1,s2);
      int matrix[s2len+1][s1len+1];
    matrix[0][0] = 0;

    for (x = 1; x <= s2len; x++)
        matrix[x][0] = matrix[x-1][0] + 1;
  for (y = 1; y <= s1len; y++)
        matrix[0][y] = matrix[0][y-1] + 1;
      for (x = 1; x <= s2len; x++)
        for (y = 1; y <= s1len; y++)
            matrix[x][y] = MIN3(matrix[x-1][y] + 1, matrix[x][y-1] + 1, matrix[x-1][y-1] + (s1[y-1] == s2[x-1] ? 0 : 1));
        similarity = matrix[s2len][s1len];
       printf("payload similarity= %d \n", similarity) ;  
}
PIEBALDconsult 23-Apr-17 13:44pm    
Seems to work for me.
Did you use a debugger to step through the code to be sure you know where the problem (if any) is?
And which compiler are you using?
Member 11112168 25-Apr-17 3:05am    
I use this code in Contiki Cooja. I used another algorithm for Levenstien.thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900