String Comparison

This is a discussion on String Comparison within the C Programming forums, part of the General Programming Boards category; Code: const char *s1 = "LOloLOLOLOLoLOL blAh"; const char *s2 = "blah"; How can I compare the right hand side ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    67

    String Comparison

    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?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    stricmp(), strcasecmp(), ComparString(), or whatever your compiler supports.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    By compare, do you mean compare, or do you mean search for a substring?

    Some implementations provide strcasecmp and strcasestr.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Compare. Similar to strnicmp, only the reverse...

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Wait... so you want it to start from the end of the string and do a case insensitive comparison?

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by master5001 View Post
    Wait... so you want it to start from the end of the string and do a case insensitive comparison?
    Exactly.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Mkay. Not explicitly, but you can always write your own or use strrev() and stricmp.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think you're going to have to roll your own here.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    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;			
    
    }

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Mkay...

    Quote Originally Posted by kpreston View Post
    Critique 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;
            }
        }
     
        //But how do you know which one is different in what way? :(
        return !same;            
     
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  13. #13
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by master5001 View Post
    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) - 1, *pe2 = p2 + strlen(p2) - 1;
        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;
    }
    Nifty. Thank you all for the quick responses.

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by tabstop View Post
    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.
    Oops. Good catch. Didn't notice my typo.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I changed a couple lines of code real quick, kpreston. I just switched the pointer--'s to --pointers to kill a -1 else where.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21