2013-07-27 104 views
0

我需要比较两个字符串是否相等(不区分大小写),但我的实现在编译时返回了很多警告。将字符串转换为小写字母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] 

我怎样才能实现这个

+1

你应该阅读的tolower的'文档()'。 – 2013-07-27 16:29:16

+1

'tolower'处理单个字符 –

回答

4

tolower不会使整个字符串小写,只是一个单一的字符。你需要把它放在一个循环中去做你正在尝试的事情。

您的系统可能具有strcasecmp(3)(UNIXy)或_stricmp(窗口)功能,这对您更为方便(尽管非标准)。

strcasecmp是POSIX中的,所以它可能是相当便携的,如果您选择该路线。

+0

我打算用评论syaing“这个问题应该被关闭,而不是回答”,但我扔掉downvoting的部分,因为它是你回答downvote答案... – 2013-07-27 16:30:22

+0

它是重复的?否则,这似乎是一个足够合理的问题;特别是因为正确的答案不是“读取'tolower'文档”,它是“使用不区分大小写的字符串比较”。 –

+0

相反,+1因为某人downvoted:P(Blehh,changin'我的心...) – 2013-07-27 16:31:22

1

使用stricmp(wordArray[i],string)

,而不是strcmp(tolower(wordArray[i]), tolower(string))

相关问题