user2240033
user2240033

Reputation: 71

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?

Upvotes: 2

Views: 277

Answers (2)

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42929

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

Upvotes: 5

Adrian Shum
Adrian Shum

Reputation: 40056

Quoted from cplusplus.com about return value of islower():

A value different from zero (i.e., true) if indeed c is a lowercase alphabetic letter. Zero (i.e., false) otherwise.

So, just do if (islower(ch)) instead of if (islower(ch) == true)

Upvotes: 5

Related Questions