0

I need to compare two strings for equality (case-insensitive) but my implementation is returning alot of warnings at compile.

my implementation:

//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;

I'm getting these warnings:

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]

How can i implement this

Kyle Hale
  • 7,912
  • 1
  • 37
  • 58
FunkyT
  • 51
  • 3
  • 8

2 Answers2

5

tolower doesn't make a whole string lowercase, just a single character. You need to put it in a loop to do what you're trying.

Your system may have a strcasecmp(3) (UNIXy) or _stricmp (windows) function, which would be more convenient for you (though non-standard).

strcasecmp is in POSIX, so it's likely to be quite portable, should you choose that route.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • I was planning to downvote the answers with a comment syaing "this question should be closed and not answered", but I throw away the downvoting part since it was you answering... –  Jul 27 '13 at 16:30
  • Is it a duplicate? Otherwise it seems like a reasonable enough question; especially since the right answer isn't "read the `tolower` docs", it's "use a case-insensitive string comparison". – Carl Norum Jul 27 '13 at 16:31
  • Conversely, +1 because someone downvoted :P (Blehh, changin' my mind...) –  Jul 27 '13 at 16:31
  • why so? If he had read it, he would have found out that `tolower()` is for converting a single character. Making assumptions instead of referring the documentation is not "reasonable enough". –  Jul 27 '13 at 16:34
  • Sure, but that would just have led him to solve the problem in the wrong way. If his question was worded "is there a case-insensitive string comparison function", would that be better? That's the subtext here. – Carl Norum Jul 27 '13 at 16:35
  • Yes, that's right. By the way, he posted a question about a very similar error, just the other way around (the message did not say "integer from pointer", but "pointer from integer"): [this one](http://stackoverflow.com/questions/17182041/assignment-makes-pointer-from-int-w-out-a-cast). Apparently, he hasn't learnt anything from the solution/answer to that question. –  Jul 27 '13 at 16:38
1

Use stricmp(wordArray[i],string)

instead of strcmp(tolower(wordArray[i]), tolower(string))

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31