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

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

    Do you need to return where there are differences in case or just whether there is a difference in case or not?

    You can compare character codes in ASCII to one another to check if your value is within a range or not.

    This code works if you don't know that the string will be only letters. You can remove some of the checks if you know that it will be only letters.

    int checkLowerAndUpper( char * string ) /* pass a null-terminated char pointer */
    {
      int i; /* loop variable */
      int length = strlen(string); /* Length */
      int foundLower = 0; /* "boolean" integers */
      int foundUpper = 0;
    
      for( i = 0; i < length; ++i ) /* Loop over the entire string */
      {
        if( string[i] >= 'a' && string[i] <= 'z' ) /* Check for lowercase */
          foundLower = 1;
        else if( string[i] >= 'A' && string[i] <= 'Z' ) /* Compare uppercase */
          foundUpper = 1;
    
        if( foundLower && foundUpper )
          return 1; /* There are multi case characters in this string */
      }
    
      return 0; /* All of the letters are one case */
    }
    

    Hopefully that helps!

    0 讨论(0)
提交回复
热议问题