Search code examples
c++comparison-operators

islower() works with "!= false" but not "== true"


So I wrote a basic program that checks for lowercase vowels in a string and displays how many it found.

I was using this at first:

for (char ch : str)
    {
        if (islower(ch) == true && isVowel(ch) == true) //isVowel is a function that
            strCount++;                                 //I made
    }

And my program wouldn't increment the counter but when I changed it to this:

for (char ch : str)
    {
        if (islower(ch) != false && isVowel(ch) == true)
            strCount++;
    }

It started working immediately. Why? Don't

if (islower(ch) != false)

and

if (islower(ch) == true)

do the exact same thing?


Solution

  • islower returns an integral value different from zero (i.e., true) if indeed ch is a lowercase alphabetic letter. Zero (i.e., false) otherwise.

    Comparing as islower(ch) == true it would be valid if islower returned 1, which as mentioned above this isn't the case.

    Consequently, rightfully islower(ch) == true doesn't work as you would expected.

    LIVE DEMO