Thread: String comparison question

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    String comparison question

    Say I have a char array called dave, with elements dave[0] = 'h' and dave[1] = 'i'

    What is the best way of finding out if they char array is equal to 'hi'?

    Is the best way of doing comparing each element at a time such as if(dave[0] == 'h' && dave[1] == 'i') or is there a C function which does this for me?

    Thanks

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Read about function named : strcmp with prototype in string.h!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    K safe.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    strcmp will work, provided both strings are exactly that - strings. A string in C is terminated with a zero byte, thus dave[2] would have to be 0. I mention that detail because you don't make it clear in your post as to whether this is indeed the case.

    Failing that, there is memcmp - compare memory for a specified number of bytes.
    I think you can put a signature here.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Hang on a minute, I'm comparing it to find out if the string is a certain word, I'm not comparing 2 different strings.

    I only have one string and I want to compare that with a variety of words.

    Is there a function that can help me with this?

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Could I do...?

    Code:
    strcmp(dave,"hi");

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I only have one string and I want to compare that with a variety of words.
    You would need to write this manually. Fortunately, it's relatively simple:
    Code:
    char buf[BUFSIZ]; /* Your string */
    
    if ( fgets ( buf, sizeof buf, stdin ) != NULL ) {
      char *words[] = {"This","is","a","test",NULL};
      size_t i;
    
      for ( i = 0; words[i] != NULL; i++ ) {
        if ( strcmp ( buf, words[i] ) == 0 ) {
          /* Matching word */
        }
      }
    }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Quote Originally Posted by Wiretron
    Could I do...?

    Code:
    strcmp(dave,"hi");
    So this wouldn't work?

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Wiretron
    Say I have a char array called dave, with elements dave[0] = 'h' and dave[1] = 'i'
    it will only work if dave[2] == '\0'
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM