Thread: strcmp

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    strcmp

    Can we always find in two strings, whether which one come first lexicographically using strcmp. I mean suppose in some character set value of 'a' is larger than 'b', then what would be the result of strcmp("a","b") ?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    When in doubt...strcmp definition
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by man strcmp
    RETURN VALUE
    The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less
    than, to match, or be greater than s2.
    Yes, you can determine lexicographical order with strcmp, but what exactly that means will depend on your encoding scheme (ASCII, EBCDIC, etc). Now, generally, this will put "A" before "B" and such, but for example, ASCII puts upper case before lower case, while EBCDIC does the reverse, and while ASCII keeps all upper case letters contiguous and all lower case letters, EBCDIC has the occasional punctuation mark in between letter groups.

    It's unlikely you're using EBCDIC, (you're probably using ASCII or some UTF encoding) but it is worth noting in case you do work on a system with a different encoding scheme.
    Last edited by anduril462; 09-21-2011 at 12:21 PM.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    @AndrewHunter
    I know that definition , but still I was confused thats why I posted here, seems like you don't understand my question.

    In C programming a modern approach author said that strcmp(s1,s2) return value smaller than 0, 0, greater than zero depending upon s1 is smaller than s2 or equal or s2 is smaller than s1.
    Now if we see "a" and "b" lexicographically than "a" is smaller than "b", but suppose in some character set 'a' s value is greater than 'b' than it should return +ve value according to standard definition, which means "a" is larger than "b" which obviously is not true as far as I can see.

    So I want clarification for this only
    Thanks
    Last edited by _arjun; 09-21-2011 at 12:29 PM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Quote Originally Posted by anduril462 View Post
    Yes, you can determine lexicographical order with strcmp, but what exactly that means will depend on your encoding scheme (ASCII, EBCDIC, etc). Now, generally, this will put "A" before "B" and such, but for example, ASCII puts upper case before lower case, while EBCDIC does the reverse, and while ASCII keeps all upper case letters contiguous and all lower case letters, EBCDIC has the occasional punctuation mark in between letter groups.

    It's unlikely you're using EBCDIC, (you're probably using ASCII or some UTF encoding) but it is worth noting in case you do work on a system with a different encoding scheme.
    Thanks

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by _arjun View Post
    Can we always find in two strings, whether which one come first lexicographically using strcmp. I mean suppose in some character set value of 'a' is larger than 'b', then what would be the result of strcmp("a","b") ?
    It would be reasonable to assume that on such an operating system/compiler setup the library functions would be adjusted to give appropriate results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp
    By lostandconfused in forum C Programming
    Replies: 3
    Last Post: 07-15-2010, 03:22 PM
  2. strcmp help...
    By Wiiplayer12 in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2008, 10:24 PM
  3. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  4. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  5. strcmp
    By joed in forum C Programming
    Replies: 4
    Last Post: 03-14-2005, 12:35 PM