user2126081
user2126081

Reputation: 285

How can I check whether the input string contains combination of uppercase and lowercase?

I would like to know how to check whether the input string contains combination of uppercase and lowercase. After that print a statement to show that the input string contains combination of uppercase and lowercase.

Upvotes: 3

Views: 35507

Answers (7)

Drakosha
Drakosha

Reputation: 12155

Iterate every char in the input string (I am assuming 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.

Upvotes: 1

Connor Hollis
Connor Hollis

Reputation: 1155

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 */
}

Upvotes: 0

WhozCraig
WhozCraig

Reputation: 66194

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

Upvotes: 3

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

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

Upvotes: 1

Puran Joshi
Puran Joshi

Reputation: 3726

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.

Upvotes: 0

salezica
salezica

Reputation: 76889

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

}

Upvotes: 4

Tuxdude
Tuxdude

Reputation: 49473

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

Upvotes: 0

Related Questions