I am trying to compare two strings to see if they are the same regardless of case. I've found a few functions. strcmp() is case sensitive but some others like stricmp() isn't in C and strcasecmp() dosen't seem to work either. Can anyone suggest a function in C which does this?
Asked
Active
Viewed 6,182 times
-2
-
Have you trie `strncmp();`? – VatsalSura Jul 31 '16 at 12:54
-
I believe that's case-sensitive, isn't it? – user1636588 Jul 31 '16 at 12:56
-
Thanks Markus, I checked but didn't see that one – user1636588 Jul 31 '16 at 12:57
-
One implementation: http://cvsweb.openbsd.org/cgi-bin/cvsweb/xenocara/app/xedit/strcasecmp.c?rev=1.1&content-type=text/x-cvsweb-markup – Kusalananda Jul 31 '16 at 13:31
1 Answers
0
int iequals(const char* a, const char* b){
unsigned int size1 = strlen(a);
if (strlen(b) != size1)
return 0;
for (unsigned int i = 0; i < size1; i++)
if (tolower(a[i]) != tolower(b[i]))
return 0;
return 1;
}
-
-
-
@LưuVĩnhPhúc : Answer has already been edited. Now using `strlen()` to compute the size of string. – Shravan40 Jul 31 '16 at 13:16
-
1I felt like expanding this example. http://hastebin.com/vikepileka.coffee – Dmytro Jul 31 '16 at 13:25
-
@Dmitry : Great work :). I should have explained that much in my function. – Shravan40 Jul 31 '16 at 13:27