strcmp()

This is a discussion on strcmp() within the C Programming forums, part of the General Programming Boards category; hi, im using strcmp to compare a character pointer char *cptr with a character array char text[100] using if(strcmp(cptr, text)==0) ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    strcmp()

    hi, im using strcmp to compare a character pointer char *cptr with a character array
    char text[100] using if(strcmp(cptr, text)==0) printf("%s", cptr);

    my debugger shows this....

    cptr = 0x0012F028 "orange" char*
    text "orange" char[]

    the result of strcmp(cptr, text)==0 will always be false... why?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Are both arrays null terminated?

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    show code where you initialize both strings
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    i used strtok to get 2 string whereby each string is seperated by SPACE. then i compare them using strcmp. is dat wat causes the error because using strtok removes the "\n" ? if so how do i actually compare the string?
    Last edited by norhos; 03-15-2008 at 02:32 AM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by norhos View Post
    i used strtok to get 2 string whereby each string is seperated by SPACE. then i compare them using strcmp. is dat wat causes the error because using strtok removes the "\n" ? if so how do i actually compare the string?
    show the code instead of your interpretation what is going on...
    Small compileable samle representing the problem
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    check out the strcmp at the last while loop
    Last edited by norhos; 03-15-2008 at 08:55 AM.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    if(strcmp(currNode->words, ptr ))
    You're missing the comparison to 0.... the one that you told us you were doing.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    even if i put if(strcmp(currNode->words, ptr ) == 0) still return false when my input is shown below is being compared

    name, value, type

    currNode->words, 0x003a1d30 "orange", char *
    ptr, 0x0012fe78 "orange", char *
    Last edited by norhos; 03-15-2008 at 03:31 AM.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    and what about sample file you are reading?
    I suppose the problem is '\n' char you do not remove so one string contains it and another - does not...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    hmmm i dun understand. which part did i removed the \n? isit the strtok ?

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by norhos View Post
    hmmm i dun understand. which part did i removed the \n? isit the strtok ?
    if one string is
    "orange apple\n"
    and second
    "apple orange\n"

    with your strtok approach you finish with comparing

    "orange" to "orange\n"
    and "apple\n" to "apple"

    both comparisomns will fail
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    no wonder.. one of my text file is like and essay and the other is one word per line...
    so.. any advise on how to solve the problem or is there other way to compare the string without using strcmp?
    Last edited by norhos; 03-15-2008 at 05:40 AM.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    yea i already fixed that. now its jus only the strcmp issue

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    need some help on my link list
    Read the last post, I've already attempted to sort some of your issues out.

    You obviously haven't done what I've suggested.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    Thanks for the advice everyone, i manage to solve it

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 05:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 12:04 PM

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