Thread: standard for comparing

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    standard for comparing

    what is the standard to compare string and ignore case, one book said strcmpi but my linux has strcasecmp, what if any is the standard?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It's trivial to do it yourself:
    Code:
    /* Change case to lower (strlwr) */
    char *lower(char *s)
    {
        char *p = s;
    
        while (*p != '\0') {
            *p = tolower(*p);
            ++p;
        }
    
        return s;
    }
    
    int compare(char *s1, char *s2)
    {
        return strcmp(lower(s1), lower(s2));
    }
    Of course, when working with string literals it isn't quite so easy. Close though.
    My best code is written with the delete key.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @Prelude: I was going to comment on how inefficient your version is, but then I realised you'd left it as an exercise for the OP.

    @chrismiceli: If you know your compilers, you could also try to #define your way out of the problem. But, I'd still recommend the creation of your own function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  2. Abstract Base Class and References
    By Thantos in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2004, 01:35 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM