Thread: strcmp without case sensitivity

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    strcmp without case sensitivity

    is there a way to use strcmp so that it is not case sensitive?
    For instance, if I call strcmp(value, "string"), and value is either String or STRING, I will still return a true.
    Thanks for your help

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    strcmpi()

    works like strcmp() , but without case sensitivity.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    Some C libraries support stricmp() -- a non-standard call -- which ignores case
    If your C doesn't have it try something like this:

    Code:
    #include <ctype.h>
    /* case blind strcmp */
    
    int stricmp (const char *p1, const char *p2)
    {
      register unsigned char *s1 = (unsigned char *) p1;
      register unsigned char *s2 = (unsigned char *) p2;
      unsigned char c1, c2;
    
      do
      {
          c1 = (unsigned char) toupper((int)*s1++);
          c2 = (unsigned char) toupper((int)*s2++);
          if (c1 == '\0')
          {
    	        return c1 - c2;
          }
      }
      while (c1 == c2);
    
      return c1 - c2;
    }

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Borland Turbo C used to have stricmp(). I have never encountered strcmpi() in a C library.
    What environment supports that?

  5. #5
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    looks like strcmpi() is C++
    strcasecmp() is C [edit] (GNU) [[/edit] and is not case sensitive.

    dinjas
    Last edited by dinjas; 03-15-2005 at 05:29 PM. Reason: added Gnu
    straight off the heap

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    strcmpi() is supported by VC++6 and DevC++ (delcared in string.h). I'm not sure if its standard or not though.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're thinking probably of stricmp, also another non-standard command. Like so.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    thanks for all of your help

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    By the way, when writing your own implementation you should avoid starting the name of the function with the characters "str" as it violates the C symbol naming standard (there's more rules than just not starting with "str"). If you try to compile that function with a compiler that has stricmp() then you'll get redefinition warnings or errors.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Basic Data types continue
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 10:21 AM