Comparing strings

This is a discussion on Comparing strings within the C Programming forums, part of the General Programming Boards category; So, I've been struggling for a while now figuring out how to properly compare strings. I have Code: typedef struct ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    Comparing strings

    So, I've been struggling for a while now figuring out how to properly compare strings. I have

    Code:
    typedef struct {
            char word[101];
            int freq;
    } WordArray;
    
    static WordArray *words[10000];
    What do I use to compare with the word in WordArray?

    I've tried

    char tempWord[101];
    tempWord = words[0]->word

    I've also tried the above except
    char *tempWord[101];
    gives me problems.

    I tried both of the above with just regular assignment

    tempword = words[0]->word;


    And many more. Any help appreciated. It would help if it was an array of temporary words but I just cant get that going >_>

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    None of those would be a comparison. At best you are assigning. To compare two strings, use strcmp, which is basically just a loop that loops through each character one at a time to see if they are equal.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I may be completely off-base here, but isn't it a better idea to do dynamic allocation (instead of fixed 10k), or:
    Code:
    typedef struct {
        char word[101];
        int freq;
    } WordArray;
    
    struct word_holder {
        struct *wordArray[10000];       /* this way, you won't be using 80 kilobytes of the stack just to hold pointers */
    };
    
    struct word_holder *array;    /* just allocate this regularly */

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    30
    Sorry, I forgot to include that strcmp gives me problems too

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    23
    You should use strcpy instead of = as in:
    strcpy(tempword,words[0]->word).

    Check this out for more info:
    Dystopian Code: Concatenating, Copying and Comparing Strings in C

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    typedef struct {
            char word[101];
            int freq;
    } WordArray;
     
    static WordArray *words[10000];
    That is an array of pointers to structures. You don't actually have any structures allocated. But if you did, it would compare something like this:
    Code:
    if( strcmp( words[ x ]->word, words[ y ]->word ) == 0 )
        ... they are the same
    If you made that an array of structures instead of pointers to structures, then it would be:
    Code:
    if( strcmp( words[ x ].word, words[ y ].word ) == 0 )
        ... they are the same
    Both of those assume you have actually copied something into 'word':
    Code:
    strcpy( words[ x ]->word, "hello" );
    strcpy( words[ y ]->word, "world" );

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    30
    But if I wanted to compare a non structure string to a structure one? strcpy(tempword,words[0]->word)? How would my tempword be declared?



    char tempWord[101]; Would this suffice?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing strings
    By JohnDeon in forum C Programming
    Replies: 3
    Last Post: 11-22-2011, 07:19 PM
  2. need help comparing strings
    By busdude in forum C Programming
    Replies: 2
    Last Post: 12-02-2009, 09:00 PM
  3. comparing strings.
    By goran00 in forum C Programming
    Replies: 3
    Last Post: 04-20-2008, 09:18 AM
  4. help comparing strings
    By molleman in forum C Programming
    Replies: 6
    Last Post: 03-07-2008, 07:09 AM
  5. Comparing 2 Strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2002, 07:01 PM

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