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 statement to show that the input string contains combination of uppercase and lowercase. Thanks in advance.
C programming: How to check whether the input string contains combination of uppercase and lowercase
-
You may want to clarify for the masses of code below whether your definition of "mixed" case is to *ignore* anything besides alphabetic characters. Ie. There is no upper-vs.-lower case for a space (ASCII 0x20) like there is for alphabet characters ('A' and 'a'). There is no mixed case for digits, etc. Is this criteria to work with all input strings including ones with no-mixed-case characters? – WhozCraig Mar 02 '13 at 07:29
-
[Most efficient way to find if a string is mixedCase](https://stackoverflow.com/q/57682576/995714) – phuclv Nov 29 '19 at 16:17
7 Answers
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);
}
- 69,920
- 24
- 96
- 160
-
3+1: Not that it matters to the OP, but this will NOT work on an AS/400 or OS/390 (and a few other platforms) due to the EBCDIC nature of those beasts. 'a'..'z' is not sequential (and not even guaranteed to be so by the C or C++ standards). Likewise with 'A'..'Z'. On ASCII platforms, however, this will work just fine. – WhozCraig Mar 02 '13 at 07:09
-
2The other problem with the `'a'..'z'` notation is that it misses `'é'`, `'ï'`, `'ô'`, `'Ÿ'`, etc; they're accented alphabetic characters but they are not represented by codes in the range `'a'..'z'`. – Jonathan Leffler Mar 02 '13 at 14:47
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;
}
- 63,603
- 10
- 71
- 134
-
Nitpick: the `strlen()` scans the string once (all 32 MB of it, perhaps) when examining just the first two characters might be sufficient. `*src != '\0'` would be sufficient as an end of string test, and avoids the unnecessary scan. – Jonathan Leffler Mar 02 '13 at 14:41
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.
- 11,617
- 4
- 38
- 49
#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;
}
- 26,910
- 19
- 95
- 126
-
`str.Length()` is not a C construct; it is an import from Java or C++ or something. – Jonathan Leffler Mar 02 '13 at 14:34
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);
- 44,385
- 13
- 103
- 107
-
Common, allow the person asking the question to learn.. I want to vote down – Drakosha Mar 02 '13 at 07:07
-
Do not write character type ranges like that (`c >= 'a' && c <= 'z')`; use the macros from `
`. There are code sets (such as EBCDIC) where there are punctuation characters inside the range `'a'..'z'`; there are also lots of alphabetic characters outside that range (`'à'`, `'ï'`, `'Ÿ'`, etc). You don't even stipulate that you're working with ASCII. – Jonathan Leffler Mar 02 '13 at 14:44
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.
- 3,416
- 2
- 12
- 9
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!
- 1,105
- 1
- 7
- 13
-
Do not write character type ranges like that (`string[i] >= 'a' && string[i] <= 'z')`; use the macros from `
`. There are code sets (such as EBCDIC) where there are punctuation characters inside the range `'a'..'z'`; there are also lots of alphabetic characters outside that range. You do stipulate that you're working with ASCII (so you don't have punctuation in those ranges of letters) but even so, it is better to use the macros. – Jonathan Leffler Mar 02 '13 at 14:37