How can I compare the right hand side of s1 to s2, case insensitively? Is there a standard function for this, or will I need to write my own?Code:const char *s1 = "LOloLOLOLOLoLOL blAh"; const char *s2 = "blah";
How can I compare the right hand side of s1 to s2, case insensitively? Is there a standard function for this, or will I need to write my own?Code:const char *s1 = "LOloLOLOLOLoLOL blAh"; const char *s2 = "blah";
stricmp(), strcasecmp(), ComparString(), or whatever your compiler supports.
By compare, do you mean compare, or do you mean search for a substring?
Some implementations provide strcasecmp and strcasestr.
Compare. Similar to strnicmp, only the reverse...
Wait... so you want it to start from the end of the string and do a case insensitive comparison?
Mkay. Not explicitly, but you can always write your own or use strrev() and stricmp.
I think you're going to have to roll your own here.
Critic my coding, please:
It appears to work, but it looks pretty damn gross.
Code:int strircmp(const char *s1, const char *s2) { // Compare right hand side of s1 with s2, case insensitively // Returns 0 if same, 1 if different int i = 0; int same = 1; for (i = (strlen(s2) - 1); i <= 0; i--) { if (tolower(s2[i]) != tolower(s1[strlen(s1) - (strlen(s1) - i)])) { same = 0; break; } } return !same; }
Here is one from good ol' Microsoft... With some alterations to fit your specs.
Example:
Code:/* Copyright (c) Microsoft Corporation. All rights reserved. */ #include <ctype.h> /* Case insensitive strcmp. Non-ISO, deprecated. */ int strricmp(const char * p1, const char *p2) { char c1, c2; const char *pe1 = p1 + strlen(p1), *pe2 = p2 + strlen(p2); int v; do { c1 = *(--pe1); c2 = *(--pe2); v = (unsigned int) tolower(c1) - (unsigned int) tolower(c2); } while ((v == 0) && (p1 <= pe1) && (p2 <= pe2) ); return v; }
Last edited by master5001; 10-20-2008 at 07:40 PM.
Why are you assuming that s2 is shorter than s1? (Or is this an implicit strnircmp?)
Since strlen(s1) isn't going to change, there's no reason to compute it 2n times. Also
s1[strlen(s1)-(strlen(s1)-i)] isn't right anyway; should be s1[strlen(s1)-(strlen(s2)-i)]. Oh, and strlen(s2) isn't going to change either.
I changed a couple lines of code real quick, kpreston. I just switched the pointer--'s to --pointers to kill a -1 else where.