What does strcmp actually do?

This is a discussion on What does strcmp actually do? within the C Programming forums, part of the General Programming Boards category; I have in my notes that strcmp will compare 2 strings with each other. But how does it do this? ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    What does strcmp actually do?

    I have in my notes that strcmp will compare 2 strings with each other. But how does it do this? I mean would it say that abc and cba are the same, or are they different things? I.e. does it just compare the letters in the string, or does the order matter?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    The order matters. Two strings are equal if and only if they have the same characters in the same order.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    just characters one by one
    Something like this: (of course it is more optimised)
    Code:
    int strcmp(char* a, char* b)
    {
       while(*a != '\0' && *b != '\0' )
       {
          if(*a != *b)
             return *b - *a;
          a++;
          b++;
       }
       return *b - *a;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    From http://src.opensolaris.org/source/xr...9/gen/strcmp.s (which is from http://www.google.ca/search?hl=en&q=...entation&meta=):
    Code:
         32 /* strcmp(s1, s2)
         33  *
         34  * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
         35  *
         36  * Fast assembler language version of the following C-program for strcmp
         37  * which represents the `standard' for the C-library.
         38  *
         39  *	int
         40  *	strcmp(s1, s2)
         41  *	register const char *s1;
         42  *	register const char *s2;
         43  *	{
         44  *
         45  *		if(s1 == s2)
         46  *			return(0);
         47  *		while(*s1 == *s2++)
         48  *			if(*s1++ == '\0')
         49  *				return(0);
         50  *		return(*s1 - s2[-1]);
         51  *	}
         52  */
    [edit] Personally, I think [-1] looks weird. [/edit]
    Last edited by dwks; 12-13-2006 at 12:32 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    http://www.cplusplus.com/ref/cstring/strcmp.html
    Return Value.
    Returns a value indicating the lexicographical relation between the strings:

    return value description
    <0 string1 is less than string2
    0 string1 is the same as string2
    >0 string1 is greater than string2
    strcmp() can tell you if the two strings are equal to each other and if not, it will tell you which one is comes first in "ASCIIbetic" order, which is a very cheap way of sorting done as simple as a > operation between two characters in the string, or substraction as illustrated above.
    This is ugly most of the time since 'A' and 'a' are different characters. You'd probably want stricmp() for better sorting, unfortunately I don't think that stricmp() is a part of the standard.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    unfortunately I don't think that stricmp() is a part of the standard.
    It isn't part of the ANSI standard, although I think it may be part of the POSIX standard.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 05:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 07:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

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