Thread: Strcmp function.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Strcmp function.

    Hello again. I have some questions here about strcmp function.

    For instance... let's go in some code.

    Assuming that we have

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
    	
    	if(strcmp("22" , "17 December Testing") < 0)
    	puts("Ok1");	
    	
    	printf("\n%d" , strcmp("25" , "23 December Testing"));	
    	
    	return 0;
    }
    I have read from the theory when we have a string that is shorter from one other is also less from this. For example if we have "abc" and "abcd" the first literal is less than the second literal.

    However I have the need to discuss what can I do when I have literals as previous. For example "25" and "23 December Testing" on this we take the value 1 or some value greater than 0 hence first literal is not less than second.

    Anyway the question is .... what it occurs when we have two strings that do not match their characters (the opposite case from "abc" , "abcd") and the second or the first is shorter.... as I wrote before .

    So far I believe that strcmp will compare the first two 25 and 23 ... 25 > 23 ... so it returns with the > 0 value... I am right ?

    Thank you in advance for your time.

    Mr. Lnx
    Last edited by Mr.Lnx; 12-12-2012 at 11:13 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can think of string length as a "tie breaker", e.g., strcmp("ab", "a") > 0 but strcmp("ab", "b") < 0 since 'a' < 'b', assuming ASCII or the like.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The rule about length that your referring to only applies when one string is a prefix of the other.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by iMalc View Post
    The rule about length that your referring to only applies when one string is a prefix of the other.
    Give one example please....

    According to laserlight I checked some examples on myown.

    Code:
     	printf("\n%d\n" , strcmp("f" , "ejh "));
    Here strcmp compares f and e at first f has greater ASCII value than e .... then we have the '\0' character of the first literal so the work of strcmpy ends here?
    Either or not the second literal "ejh" has more than n-1 character from the first .

    what about when we have

    Code:
     printf("\n%d\n" , strcmp("  a" , "a  "));
    strcmp compares space with a then space with space then a with space ???
    Last edited by Mr.Lnx; 12-12-2012 at 12:41 PM.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Mr.Lnx View Post
    Code:
         printf("\n%d\n" , strcmp("f" , "ejh "));
    Here strcmp compares f and e at first f has greater ASCII value than e .... then we have the '\0' character of the first literal so the work of strcmpy ends here?
    The comparison ends at the first mismatch -- its that simple. What's all this about the length of the string and such...

    Compare "f" and "ejh ":

    Index 0: 'f' > 'e' therefore strcmp ends with result: >0 (positive integer for result means the left is greater than the right)

    By the way, you can get the result of strcmp by just doing a simple calculation on the first letters that mismatch: 'f'-'e' obviously results in a positive number, so the result from strcmp will be positive. If they are equal... the comparison continues.

    Compare "aaa" and "aa"

    Index 0: 'a' == 'a'
    Index 1: 'a' == 'a'
    Index 2: 'a' > '\0', so again the comparison ends with a positive number because 'a'-'\0' is positive

    Compare " a" and "a ":

    Index 0: ' ' < 'a', therefore the comparison ends with a negative number because ' '-'a' is negative
    Last edited by c99tutorial; 12-12-2012 at 12:50 PM.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Very good explanation thank you but I have some last questions....

    Consider "22" and "a" will give negative result from strcmp ..... I think function compares 'a' with '2' not 'a' with '22' ... and then ends

    that is the same with "22" and "33" ? I mean again will compare with '2' and '3' and '2' and '3' ???

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes, character by character. strcmp does not interpret decimal numbers (multiple digits) in any more intelligent form. It just goes by comparing individual corresponding characters according to their magnitudes in the ASCII chart ( Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion ).

    Compare '2' with 'a': '2' is 50, and 'a' is 97. 50 - 97 is negative.

    In your second example, no it will not "compare with '2' and '3' and '2' and '3' ". It will do the first comparison and stop because the characters are different. There is no need to check further.
    Last edited by nonoob; 12-12-2012 at 04:20 PM.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Very well. Thank you ... it is clear now

    EDIT: Sorry me guys.... really something last :P

    "abc" , "abcd"

    The comparisons are:

    'a'=='a'
    'b' == 'b'
    'c' == 'c'
    '\0' < 'd'

    so negative result ?! ok

    forget it..... is the same example with "aaa" and "aa" as c99tutorial wrote before... thanks again.
    Last edited by Mr.Lnx; 12-12-2012 at 05:35 PM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The ordering strcmp uses is called Lexicographical Ordering.
    Please have a read of the wikipedia article on it: Lexicographical order - Wikipedia, the free encyclopedia
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my strcmp and strrev own function
    By irwebnews in forum C Programming
    Replies: 10
    Last Post: 05-18-2012, 07:14 AM
  2. help with write your own strcmp function
    By Iris Li in forum C Programming
    Replies: 1
    Last Post: 05-03-2012, 08:47 AM
  3. how to modify strcmp function
    By asteroid1122 in forum C Programming
    Replies: 6
    Last Post: 08-23-2009, 12:24 AM
  4. question about function strcmp() in C
    By thungmail in forum C Programming
    Replies: 5
    Last Post: 03-13-2008, 05:46 AM
  5. strcmp
    By tat in forum C Programming
    Replies: 8
    Last Post: 12-05-2006, 12:07 AM