该函数在一个字符串中找到可能的最长的子字符串,该字符串是由同一字符组成的

#include 
#include 

char *my_max(char *p, char *q)
{
    int i=0,j=0,len=0,max=0;
    char *a,*b;

    a = (char *)malloc(sizeof(char)*20);
    b = (char *)malloc(sizeof(char)*20);
    while(*p != '\0') {
        len=0;
        while((*q!='\0') && (*q!=*p)) {
            q++;
        }
        if(*q == '\0')
            continue;
        
        while(*p==*q) {
            b[len]=*p;
            p++;
            q++;
            len++;
        }
        if(max<len) {
            max = len;
            for(i=0;b[i]!='\0';i++) {
                a[i]=b[i];
            }
            a[len]='\0';
        }
        p++;
    }
    return a;
}

int main(int argc, char const *argv[])
{
    char a[] = "abcdefabce";
    char b[] = "abcdefdabce";
    char *p;

    p = my_max(a, b);
    printf("%s\n", p);

    return 0;
}

你可能感兴趣的:(c语言)