520. Detect Capital - 单词大小写合法性检测

hhttps://leetcode.com/problems/detect-capital/

分析

简单的字符串合法性检测,只能是首字母大写或全字母大写。

bool detectCapitalUse(char* word) {
    int wordLen = strlen(word);
    int i = 0;
    int stat = 0;

    if ((word[0] >= 'A') && (word[0] <= 'Z'))
    {
        if (word[wordLen - 1] >= 'A' && word[wordLen - 1] <= 'Z')
        {
            /* 所有大写 */
            stat = 1;
        }
        else
        {
            /* 后续小写 */
            stat = 2;
        }
    }
    else
    {
        /* 所有小写 */
        stat = 3;
    }

    for (i = 1; i < wordLen; i++)
    {
        if (stat == 1)
        {
            if ((word[i] >= 'a') && (word[i] <= 'z'))
            {
                return false;
            }
        }
        else
        {
            if ((word[i] >= 'A') && (word[i] <= 'Z'))
            {
                return false;
            }
        }
    }

    return true;
}

猜你喜欢

转载自blog.csdn.net/qq_25077833/article/details/61578738