Thread: comparing two strings

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    comparing two strings

    I want to compare two strings and see if they are the same or not

    Code:
    char myString[]="hello";
    
    if (myString == "hello") {
    
       printf ("same\n');
    
    }
    the word same didn't appear after I run this program, is it because I am not supposed to use equal equal to compare?

    I know that in java, I have to use dot equal, what about in C?

    Or I have to do something like that:
    myString[0]='h'
    myString[1]='e'
    myString[2]='l'

    etc...?

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    please read up on the strcmp function, you need to compare each letter of the string individually.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    If you don't want to use strcmp(), the first thing you have to do is to check if the two strings have the same number of letters(you could use strlen() for this), then iterate over each letter of the two strings and check whether they are equal or not, of course if they are not equal you just have to break the loop, otherwise if they both reach the null character they are equal

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    The method is as follows:

    Code:
    strcmp(char String[], char String2[]);
    it returns -1 if first is before, 1 if second is before, 0 if equal

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    if u dont want to use strcmp() use a loop

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Soulzityr
    it returns -1 if first is before, 1 if second is before, 0 if equal
    By the way, please do not rely on this. Rely only on comparison for less than, equal to, and greater than 0.
    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

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Soulzityr View Post
    The method is as follows:

    Code:
    strcmp(char String[], char String2[]);
    it returns -1 if first is before, 1 if second is before, 0 if equal
    you are right about 0... 1 and -1 are not guaranteed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    strcmp() is not a "method" , it's a standard C function, have some respect.
    Methods are for cool boarders, and .NET'ers.

    Here are some ideas to kick around:
    Code:
    /*Yes , well a string is in a character array right?  eg: */
    #include <stdio.h>
    
    int main(void)
    {
       int i;
      char s1[] = "Hello";      
      char s2[] = { 'H', 'e', 'l', 'l', 'o', '\0', '\0', '\0' };
    
    /*
    Strings defined in double quotes are automatically appended with the null character: '\0' (zero).
    The string declarations above stored identically except that s2 will be two bytes larger.
    Their "sizes" are  6 and 8 respectively but their string lengths are  both 5. 
    */
    
      printf("sizes: s1: %u    s2: %u  \n\n", sizeof(s1),  sizeof(s2) );
    
    /*
    We "index" (number) the string characters, like any array, starting with zero.
    So you could make a loop and print the characters from index 0 until you run into the '\0':    
    */
    
      for ( i= 0; i < (int)sizeof(s2); i++ )
      {
        printf("s2[%d] = %c  %02x \n", i, s2[i], s2[i]); 
      } 
      putchar('\n');
    
    /* We could also make a loop to print the same index of both s1 and s2: */
    
      for ( i= 0; ; i++ )
      {
        /* stop when the first \0 of either string is encountered */
        if ( s1[i] == '\0' || s2[i] == '\0' )
        {
          break;
        }
        printf("s1[%d] = %c  %02x  ,  s1[%d] = %c  %02x \n", 
                                              i, s1[i], s1[i], i, s2[i], s2[i] );
      } 
    
      printf("\nThe shorter string's length is: %d \n", i );
    
      printf("\nIt doesn't take too much imagination to see how the second loop \n\
    could be used to compare two strings and calculate a meaningful value. \n\n\
    ...and from there taken to a function that returns a meaningful value! \n");
    
      return 0;
    }
    /*****  output :  ********************
    sizes: s1: 6    s2: 8  
    
    s2[0] = H  48 
    s2[1] = e  65 
    s2[2] = l  6c 
    s2[3] = l  6c 
    s2[4] = o  6f 
    s2[5] =   00 
    s2[6] =   00 
    s2[7] =   00 
    
    s1[0] = H  48  ,  s1[0] = H  48 
    s1[1] = e  65  ,  s1[1] = e  65 
    s1[2] = l  6c  ,  s1[2] = l  6c 
    s1[3] = l  6c  ,  s1[3] = l  6c 
    s1[4] = o  6f  ,  s1[4] = o  6f 
    
    The shorter string's length is: 5 
    ***********************************/
    Now try fooling around with the characters is s1 and s2....
    And yes you should look up strcmp(). Here is a nice C reference: :The C Library Reference Guide
    And NOWHERE in there are "methods" mentioned !!! whippersnappers!
    Last edited by HowardL; 03-30-2010 at 09:28 AM.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    okay, sorry i used the word method. i meant function, and it does not return -1 and 1 all the time

  10. #10
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Yes, a fancy true / false is what it returns:
    Returns zero if str1 and str2 are equal.
    Returns less than zero or greater than zero if str1 is less than or greater than str2 respectively.

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