Thread: strcmp

  1. #1
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59

    strcmp

    Is the standard library strcmp function fairly efficient, or are there faster strcmp methods out there?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Any given implementation is free to implement strcmp() however it chooses, so long as it conforms to the interface described in the standard.

    It's generally best to assume that whoever implemented it for your particular OS/Compiler knew what they were doing, and it will be reasonably efficient.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Cool strcmp

    all the possible libraries use the following method to compare two strings
    Code:
    for(i=1;i<strlen(smaller of the strings);i++)
    {
                   int count=0
                   if(string[i]!=string2[i])
                   {
                                  count++
                   }
    }
    return count;
    i dont think there could be a code better than this.....

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Maybe not.
    This is the GNU version of strcmp()
    Code:
    /* Compare S1 and S2, returning less than, equal to or
       greater than zero if S1 is lexicographically less than,
       equal to or greater than S2.  */
    int
    strcmp (p1, p2)
         const char *p1;
         const char *p2;
    {
      register const unsigned char *s1 = (const unsigned char *) p1;
      register const unsigned char *s2 = (const unsigned char *) p2;
      unsigned reg_char c1, c2;
    
      do
        {
          c1 = (unsigned char) *s1++;
          c2 = (unsigned char) *s2++;
          if (c1 == '\0')
    	return c1 - c2;
        }
      while (c1 == c2);
    
      return c1 - c2;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > for(i=1;i<strlen(smaller of the strings);i++)
    1. arrays start at 0,
    2. no serious attempt would bother calling strlen in advance, since it's easy to just test the current characters with '\0' anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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, 06: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, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM