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

后端 未结 7 867
旧时难觅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:03

    Something like this (which will work on both ASCII and EBCDIC platforms):

    #include <ctype.h>
    
    int hasMixedCase(const char *src)
    {
        int hasUpper=0, hasLower=0;
        for (;*src && !(hasUpper && hasLower);++src)
        {
            hasUpper = hasUpper || (isalpha(*src) && *src == toupper(*src));
            hasLower = hasLower || (isalpha(*src) && *src == tolower(*src));
        }
        return hasLower && hasUpper;
    }
    
    0 讨论(0)
  • 2021-01-23 01:08
    #include <stdio.h>
    #include <ctype.h>
    
    int main ()
    {
    
      char* str="Test String.\n";
      int Uflag=0;
      int Lflag=0;
      char c;
      for (int i=0; i<str.length(); ++i)
      {
        c=str[i];
        if (islower(c))
          Lflag=1;
        if (isupper(c))
           Uflag=1;
    
        if(Lflag!=0 && Uflag!=0)
         {
           printf("String contains combo of Upper and Lowercase letter");
           break;  // both upper case and lower case letter found , no need to iterate further.
         }
      }
      return 0;
    }
    
    0 讨论(0)
  • 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);
    
    }
    
    0 讨论(0)
  • 2021-01-23 01:17

    Iterate every char in the input string (i am assuming it's homework and it's ASCII) and check whether the char is lower case letter. In this case, set to true a variable which marks whether lower case letter was met. Do the same for upper case (or you could do it in the same loop). Then form your output based on the two boolean variables.

    0 讨论(0)
  • 2021-01-23 01:20

    You can easily do it using the ASCII value.

    Here are 2 algorithm that you can code:

    1. Intialize two variable lowerCase as false and upperCase as false.
    2. Select each character from the input string.
       2.a. Get the ascii value for that character
       2.b. If greater or equal to 97 then set lowercase as true. else set upper case as true.
    3. If end result contains upperCase as well as lowerCase as true than it contains combination of upper and lowercase.
    

    The other way much simpler is.

     1. convert the given string to lowerCase.
     2. check if it is equal to actual string if true then it is in lowerCase and return.
     3. Convert actual string to upperCase and compare again to actual string
     4. If equal than string in upperCase else it is combination of upper and lowercase.
    
    0 讨论(0)
  • 2021-01-23 01:24
    unsigned int len = strlen(inputStr);
    bool containsUpperCase = false;
    bool containsLowerCase = false;
    
    for (int i = 0; i < len && !(containsUpperCase && containsLowerCase); ++i)
    {
        char c = inputStr[i];
        if (c >= 'A' && c <= 'Z')
            containsUpperCase = true;
        else  if (c >= 'a' && c <= 'z'))
            containsLowerCase = true;
    }
    
    printf("Contains Upper Case: %d Contains Lower Case: %d\n",
           containsUpperCase, containsLowerCase);
    
    0 讨论(0)
提交回复
热议问题