Welcome to RenEvo Sign in | Join | Help
in
Home Wiisis Blogs Forums Photos Downloads About

Trying to check for an exact word

Last post 12-15-2006, 11:19 AM by vloktboky. 5 replies.
Sort Posts: Previous Next
  •  12-12-2006, 5:51 AM 398

    Trying to check for an exact word

    Hi,

     I'm trying to read this:

    char *word3;

    word3 = strtok(element, delimit);

    if (word3 == ":HELLO") {

         /* Execute the code */

    }

     

    But it doesn't read the if, I must make strstr, but it doesn't help me. It must read the exact word.

    The debugger tells me the value of word3 is

    + word3 0x00359322 ":HELLO" char *

     

     

    Thanks to anyone who can help me set this.

     

    EDIT: I can manage to read some, with

    if (string(word1) == "word") { ... }
    BUT it doesn't work with the word ":HELLO" because they are 2 squares (bytes) after the ":HELLO" in the debugger, which are empty bytes, because I use strtok() to get ":HELLO" working fine when something is behind ":HELLO" otherwise adds to space or empty bytes after it.

  •  12-13-2006, 9:38 AM 407 in reply to 398

    Re: Trying to check for an exact word

    You need to change your if statement to:

    if (strcmp(word3, ":HELLO") == 0)

    This is because when you do "word3 == ":HELLO"" you're comparing the memory address that word3 is storing to the address of the constant ":HELLO" (which will never be the same). Luckily, you can use a handy function called strcmp which takes two const char* and iterates through both strings checking if both the chars are the same. strcmp returns 0 if they are exactly the same, and a nonzero value otherwise.
     

  •  12-13-2006, 11:03 AM 408 in reply to 407

    Re: Trying to check for an exact word

    Thanks it works, but sometimes they are empty bytes, when my word isn't followed by nothing after it on the line. Should I remove them with a loop or is there a function for it please ?
  •  12-13-2006, 11:18 AM 409 in reply to 408

    Re: Trying to check for an exact word

    Compare only the characters you care about. If you know there may be a point where more than what you want will lie inside of your string, only check the characters that you need.  Use strncmp, starting at the offset into the string for the start and the count of characters in that you want.

    In your case: strncmp(word3, ":HELLO", 6);

    This will check (starting at the memory address stored in word3) the first 6 characters, and compare those to ":HELLO".  Anything from the 7th character on will be ignored. 

  •  12-13-2006, 12:22 PM 411 in reply to 409

    Re: Trying to check for an exact word

    *EDIT*: Got it to work, I made a typo before.

     

    Thanks guys !

  •  12-15-2006, 11:19 AM 445 in reply to 411

    Re: Trying to check for an exact word

    I was going to say.. :P
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems