繁体   English   中英

在c中转换为小写字母后比较字符串

[英]compare strings after converting to lower case in c

我需要比较两个字符串是否相等(不区分大小写),但是我的实现在编译时返回了很多警告。

我的实现:

//The word array will contain any number of strings of varying lengths
//string is the word to compare to
char **wordArray, char*string;

int i, sizeOfArray = 10

for(i = 0; i < 10; i++)
{
    //Return 1 if the string is seen in the array 
    if(strcmp(tolower(wordArray[i]), tolower(string)) == 0)
        return 1;
}

return 0;

我收到以下警告:

warning: passing argument 1 of ‘tolower’ makes integer from pointer without a cast [enabled by default]

note: expected ‘int’ but argument is of type ‘char *’

initialization makes pointer from integer without a cast [enabled by default]

我该如何实施

tolower不会使整个字符串变成小写,而只是一个字符。 您需要将其循环以执行您要尝试的操作。

您的系统可能具有strcasecmp(3) (UNIXy)或_stricmp (windows)函数,这对您来说更方便(尽管是非标准的)。

strcasecmp POSIX,所以很可能是相当便携的,你应该选择这条路线。

使用stricmp(wordArray[i],string)

代替strcmp(tolower(wordArray[i]), tolower(string))

暂无
暂无

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

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