Thread: Comparing strings...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Comparing strings...

    Hi,

    I tried comparing the strings as follows but it doens't work. Pls tell me the correct way, thnx a lot:

    Code:
    char string = "yyy";
    
    if ( string == "yyy" )
       printf( "match" );
    else
       printf( "doesn't match" );

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    you cant use the equality operator on strings it doesnt work, use this method

    #include<string.h>

    char[32] string1;
    char[32] string2;

    if(strcmp(string1, string2)==NULL)
    {
    cout<<"they are equal";
    }
    Monday - what a way to spend a seventh of your life

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    so i can't do it just by ordinary code or do it by scratch is that what you mean?

    thnx

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    I could be wrong, but K & R says strcmp returns 0 when they're equal.

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    k thnx

  6. #6
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    yep sorry,my mistake
    that part of the code should have been

    !=NULL

    Monday - what a way to spend a seventh of your life

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    I think you actually want:

    if(strcmp(string1, string2)==0)
    {
    cout<<"they are equal";
    }

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>so i can't do it just by ordinary code or do it by scratch is that what you mean?

    Yes you can.

    First the strings must be the same length ( strlen() or lstrlen() ) and then you will have to loop thru each string testing each index individually.

    Code:
    if(lstrlen(sString1)!=lstrlen(sString2))
         return FALSE;//no match
    for(i=0;i<lstrlen(sString1);i++)
    {
         if(sString1[i]!=sString2[i])
              return FALSE;//no match so exit
    }
    return TRUE;//got this far must be match
    As you can see using strcmp() is easier.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, you could either use strcmp or test how novacain said to. The reason being is when you say:
    Code:
    if (string1 == string2)
    These are char arrays and when you don't use the brackets to specify an element, then you start comparing addresses of the arrays. Of course, unless they are at the same address in mem, it will always turn out, "no".
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  4. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 12:10 PM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 11:41 PM