欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

怎么找到最长子串位置

程序员文章站 2022-05-01 17:52:18
怎么找到最长子串位置 #include #include #include using namespace std; //--------------------------...
怎么找到最长子串位置

#include 
#include 
#include 
using namespace std;
//---------------------------------

char* findmaxstr(char* ch)
{
    char *maxp, *temp;
    int maxn, currentmaxn;
    maxp = temp = ch;
    maxn = currentmaxn = 1;

    while (*ch) {

        if (*(ch + 1) == (*ch) + 1) {
            currentmaxn++;
        } else {

            if (currentmaxn > maxn) {
                maxn = currentmaxn;
                maxp = temp;
            }
            temp = ch + 1;
            currentmaxn = 1;
        }
        ch++;
    }

    *(maxp + maxn) = '\0';
    return maxp;
}

int main()
{

    char sourcestr[] = "abcdewrdfadq12345678000";



    printf("result: %s", findmaxstr(sourcestr));
}