简体   繁体   中英

Detecting length of overlap between two strings

int overlap(const char *s1, const char *s2){
    int i = 0;
    while (s1[i] && s2[i] && s1[i] == s2[i])
        i++;
    return i;
}

This returns the length of the substring overlap between the two strings it takes as input. However, if the two strings are:

abcdefg
1234efg

it returns an overlap of 0 because it can only read overlaps that start at the beginning of the strings, can someone modify or help me to make it so that it can read overlaps no mantter where they are in the strings?

The easy way to do this is to build a suffix-tree for the two strings(this is done using McCreght ). now just look for the longests common substring with origin in both strings.

i think the codes will be like this:

int overlap(const char *s1, const char *s2){
    int i = 0, n = 0;
    while (s1[i] && s2[i]) {
        if (s1[i++] == s2[i++]) n++;
    }
    return n;
}

Looks a little bit like homework to me, is it?

You'r while clause exits as soon as it discovers a difference between the strings. You will have to loop over the entire string, and for each index i count it if s1[i] == s2[i] .

Assuming that you want an overlap at the same index in each string, as in your example:

int overlap(const char *s1, const char *s2){
    int length = 0;
    int i = 0;
    while (s1[i] && s2[i] && s1[i] != s2[i])
        i++;
    while (s1[i] && s2[i] && s1[i] == s2[i]) {
        i++;
        length++;
    }
    return length;
}

will do the trick, although it's not very elegant. It will find the length of the first overlap at the same offset.

So for abcdefgh90 and 1234efg890 it will return 3.

If you want the total number of matching characters then try:

int overlap(const char *s1, const char *s2){
    int length = 0;
    int i = 0;
    while (s1[i] && s2[i]) {
        if (s1[i] == s2[i]) {
            length++;
        }
        i++;
    }
    return length;
}

well i have thought this question again. i think you want an overlap at the same index in each string. pay attention to the character '\\0' in the end of each string.

so we can write the codes as the following:

int overlap(const char *s1, const char *s2){
    int i = 0;
    while (*s1 != '\0' && *s2 != '\0') {
        if (*s1++ == *s2++) i++;
    }
    return i;
}

for "abcdefg" and "1234efg", it will return 3.

int overlap(const char *s1, const char *s2){
    int k;
    for(k = 0; s1[k] != s2[k]; k++)  // <--- add this loop
      if(0 == s1[k] || 0 == s2[k])
        return 0;
    int i = k;     // initialize to k + 1
    while (s1[i] && s1[i] == s2[i])  // check only s1 (or s2)
        i++;
    return i - k;  // return difference
}

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