Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Whats the difference between a range of a set(a,b) and union of all elements of sets in (a,b)?

Beginner here. Wrote a program to check if entered alphabet is lowercase or upppercase. (Practising if/else questions)
I thought of two methods:

  1. Range method-getting the desired result
  2. Union method-not getting the desired result

Here is the code,

if (enteredAlphabet>='a' && enteredAlphabet <='z')
{
    cout<<enteredAlphabet<<" is lowercase.";
}

and

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if (enteredAlphabet==('a'||'b'||'c'||'d'||'e'||'f'||'g'||'h'||'i'||'j'||'k'||'l'||'m'||'n'||'o'||'p'||'q'||'r'||'s'||'t'||'u'||'v'||'w'||'x'||'y'||'z'))
{
    cout<<enteredAlphabet<<" is lowercase.";
}

>Solution :

The expression 'a'||'b'||... is actually a short-hand for ('a' != 0) || ('b' != 0) || ....

The result of the expression will always be the boolean value true.

Since true can be converted to the integer value 1 your comparison is really the same as enteredAlphabet == 1. This comparison will likely never be true.

What you actually should use is std::islower:

if (std::islower(enteredAlphabet)) { /* Is lower case letter */ }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading