standard for comparing

This is a discussion on standard for comparing within the C Programming forums, part of the General Programming Boards category; what is the standard to compare string and ignore case, one book said strcmpi but my linux has strcasecmp, what ...

  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,796
    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, 01:02 AM
  2. Abstract Base Class and References
    By Thantos in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2004, 02:35 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM
  5. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 05:21 AM

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