C programming: How to check whether the input string contains combination of uppercase and lowercase

后端 未结 7 936
旧时难觅i
旧时难觅i 2021-01-23 00:25

I\'m totally newbie here. As stated above, I would like to know how to check whether the input string contains combination of uppercase and lowercase. After that print a stateme

7条回答
  •  耶瑟儿~
    2021-01-23 01:16

    Step 0: variables you need

    char* str;
    int   i;
    char  found_lower, found_upper;
    

    Step 1: iterate through the string

    for (int i = 0; str[i] != '\0'; i++)
    

    Step 2: detect upper and lower case characters

    found_lower = found_lower || (str[i] >= 'a' && str[i] <= 'z')
    found_upper = found_upper || (str[i] >= 'A' && str[i] <= 'Z')
    

    Step 3: combine the results

    mixed_case = found_lower && found_upper
    

    Step 4 (optional) break out of the for early to save some time

    if (found_lower && found_upper) break;
    

    Full source (warning: untested):

    char is_mixed(char* str) {
    
        int   i;
        char  found_lower = false, found_upper = false;
    
        for (int i = 0; str[i] != '\0'; i++) {
            found_lower = found_lower || (str[i] >= 'a' && str[i] <= 'z');
            found_upper = found_upper || (str[i] >= 'A' && str[i] <= 'Z');
    
            if (found_lower && found_upper) break;
        }
    
        return (found_lower && found_upper);
    
    }
    

提交回复
热议问题