简体   繁体   English

islower()适用于!=运算符,但不适用==运算符

[英]islower() works with != operator but not == operator

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? 完全一样吗?

islower returns an integral value different from zero (ie, true ) if indeed ch is a lowercase alphabetic letter. 如果ch实际上是小写字母,则islower返回一个不同于零的整数值(即true )。 Zero (ie, false ) otherwise. 否则为零(即false )。

Comparing as islower(ch) == true it would be valid if islower returned 1 , which as mentioned above this isn't the case. islower(ch) == true比较,如果islower返回1是有效的,如上所述,事实并非如此。

Consequently, rightfully islower(ch) == true doesn't work as you would expected. 因此,正确地islower(ch) == true无法正常工作。

LIVE DEMO 现场演示

Quoted from cplusplus.com about return value of islower() : 引用cplusplus.com关于islower()返回值:

A value different from zero (ie, true) if indeed c is a lowercase alphabetic letter. 如果确实c是小写字母,则该值不同于零(即true)。 Zero (ie, false) otherwise. 否则为零(即false)。

So, just do if (islower(ch)) instead of if (islower(ch) == true) 因此,只需执行if (islower(ch))而不是if (islower(ch) == true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM