Comparing ASCII code

I have to write a program where I have to detect how many uppercase letters the inputted string contains.

Normally I can do this by making a very large if statement, where each char is compared to 'A' || 'B' || 'C' || 'D' || 'E' || 'F'|| etc.

Is there an easier way to do this? I was thinking if I can compare the char to their respective ASCII code, something like;

if ( c >= 65 && c <= 90)

where it would test the ascii code of A to Z

> I was thinking if I can compare the char to their respective ASCII code

You can; it would work as expected if the current C locale uses an ASCII encoding.

if( std::isupper(c) ) would work everywhere.
http://en.cppreference.com/w/cpp/string/byte/isupper
For my assignment I have to write it as an if-else statement using conditions, can't use string predefined functions.

Is there any way to check it as an if-else condition?
> I have to write it as an if-else statement using conditions

1
2
if( std::isupper(c) ) ++n_uppercase_chars ;
else ++n_other_chars ;
I cannot use predefined string fuctions such as isupper
If you have studied arrays, you could check if the character is present in an array of upper case characters:

const char[] upper_case_chars[] = "ABCDEFGHIJKLMNOPQSTUVWXYZ" ;

Otherwise, settle for: if ( c >= 'A' && c <= 'Z' )

Ah ok thanks!

Topic archived. No new replies allowed.