2013-07-27 49 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))

相關問題