[英]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
无法正常工作。
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.