Strcmp

This is a discussion on Strcmp within the C Programming forums, part of the General Programming Boards category; How does it work? I want to write a function for it for myself. I have the string "$$$$" , ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Strcmp

    How does it work? I want to write a function for it for myself.

    I have the string "$$$$" , without quotes needed to stop a program from looping, but how do I convert the $$$$ to a number\value so I could type

    while (END!=$$$$) blah blah

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It basically just loops through each character and sees if they're equal. If every character is equal, then the strings are equal.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    So is this an assignment for school where you are not allowed to use the string.h header? Just curious.

    ~/

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    10

    I wrote one for you

    Code:
    //if str1 is bigger return a positive number,
    //if str2 is bigger return a negative number,
    //equal reutrn 0
    int strcmp(char *str1,char *str2)
    {
       while(1)
       {
          if(*str1==*str2)
          {
             if(*str1=='\0')return 0;
    
             str1++;
             str2++;
             continue;
             
          }
          else
          {
             return *str1-*str2;
          }
       }
       
    }
    
    
    main()
    {
       printf("%d",strcmp("abc","abd"));    // print -1
       getch();
    
    }

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    what does the * in (*str1==*str2) do?

    and no we cant use strcmp cause well, we didn't reach there yet.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    str1 is a pointer, its value is a memory address,
    *str1 is a char which str1 point to.

    I don't understand your second sentence,sorry.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Hey,
    I always wondered this why does the strcmp int
    <string.h>
    return 0 if they compare equally so you cant do this
    while(strcmp(str1,str2))

    this would run if they were not equal Just wondering why the person who wrote that macro did that did he do it for any particular reason

  8. #8
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    > Just wondering why the person who wrote that macro did that did he do it for any particular reason

    Upon completion, strcmp() returns an integer greater than, equal to or less than 0, if the string pointed to by s1 is greater than, equal to or less than the string pointed to by s2 respectively.

    sometimes you need to know which string is greater and which is less

  9. #9
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    for strcmp, that is right, it returns 0 if they are equal. This may seem really messed up, but it actually helps out a LOT

    One way I found that strcmp is REALLY helpful is in a sorting algorithm. Say you have a list of strings and you need them alphabetically, right? well, right away you'd think, ok, do a loop through 'em all and use some algorithm to place them in the correct order depending on their first letter. Well, what if the next few letters are different? then you'll have to search those too! so, basically strcmp does this all for you, and very nicely too

    so, for example:

    strcmp("letter a","letter b");
    would return -1, because string one is "less than" string two, and this works vice-versa too, and for any size of string! (it's a very handy function, you just have to know where to use it)

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    linuxdude wrote
    I always wondered this why does the strcmp int
    <string.h>
    return 0 if they compare equally so you cant do this
    while(strcmp(str1,str2))
    You could do

    while(!strcmp(str1,str2))

    which would mean the loop would keep going while the strings were equal.

    hobo

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >while(!strcmp(str1,str2))
    You could...but the above can be confusing. Logical not is usually associated with false, so your loop is most easily read as "while str1 and str2 are not equal", which is the exact opposite of what you're really testing for. IMO it's better to be explicit when such confusion can occur:
    Code:
    while ( strcmp ( str1, str2 ) == 0 ) /* While the strings are equal */
    Code:
    while ( strcmp ( str1, str2 ) != 0 ) /* While the strings are not equal */
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    I take your point Prelude.

    It amazes me how many things that I've been taught are either not good practice or just plain wrong and I'm obviously not the only one, going by the numerous posts that I've read. That's what makes this site so invaluable.

    Thanks Prelude
    hobo

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, 05:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01: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, 12:04 PM

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