我试图比较两个字符串中的两个特定字符,但是我想要使比较不区分大小写的。我该怎么做?
现在我正在使用这样的代码:
if (str1[i]==str2[j]) printf("Equal");但我想做这件事而不考虑案件的敏感性。
提前感谢您抽出时间来帮忙!
发布于 2019-03-17 14:29:58
您可以对两个字符使用小写,例如,通过使用tolower函数:
if (tolower(str1[i])==tolower(str2[j])) printf("Equal");还请记住: tolower不适用于多字节字符。因此,对于这些字符,您应该使用其他函数。
发布于 2019-03-17 14:38:51
我们可以通过使用toupper()或tolower()将两个字符转换为大写字符或小写字符来实现您的需求。
示例:
#include <stdio.h>
#include <ctype.h> //For tolower()
int main()
{
char str1[]="Time", str2[]="time";
/*
* Just for an example i am comparing the first char
* from 2 different strings.
*/
if(tolower(str1[0]) ==tolower(str2[0])) {
printf("Char's are equal\n");
}
else {
printf("Char's are not equal");
}
return 0;
}输出:
查尔
https://stackoverflow.com/questions/55208116
复制相似问题