服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 华为面试题答案找出最大长度子字符串

华为面试题答案找出最大长度子字符串

2021-01-11 15:39C语言教程网 C/C++

华为面试题:找出最大长度子字符串,打印并且返回长度。 例如 str = "abc123abcd234abcdefgha324adsdawqdasdaseqqwe345abchded",看下面的代码实现吧

复制代码 代码如下:


int findMaxSubstring(char* str)
{
    int maxLength = 0;
    int maxStartIndex = 0;
    int curLength = 0;
    int curStartIndex = 0;
    bool isFind = 0;
    for(unsigned int i = 0;i<strlen(str);i++)
    {
        if(str[i] >= 'a' && str[i] <= 'z')
        {
            if(isFind == 0)
            {
                isFind = 1;
                curLength = 1;
                curStartIndex = i;
            }
            else
            {
                curLength++;
            }
        }
        else if (str[i] < 'a' || str[i] > 'z')
        {
           isFind = 0;
           if(curLength > maxLength)
           {
              maxLength = curLength;
              maxStartIndex = curStartIndex;
              curLength = 0;
           }
        }
    }
    char *p = NULL;
    p = &str[maxStartIndex];
    while(*p >= 'a' && *p <= 'z')
    {
        putchar(*p);
        p++;
    }
    return maxLength;
}

延伸 · 阅读

精彩推荐