Detecting length of overlap between two strings

纵然是瞬间 提交于 2019-12-11 07:29:20

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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].




回答5:


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



回答6:


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
}


来源:https://stackoverflow.com/questions/6026483/detecting-length-of-overlap-between-two-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!