首页 > 编程 > C > 正文

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

2020-01-26 15:39:45
字体:
来源:转载
供稿:网友

复制代码 代码如下:

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选