strstr() - Not able to get it to work.

This is a discussion on strstr() - Not able to get it to work. within the C Programming forums, part of the General Programming Boards category; Hi. It's me again. Can anyone figure why the first code listing, doesnt work despite passing in the correct values? ...

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

    strstr() - Not able to get it to work.

    Hi. It's me again. Can anyone figure why the first code listing, doesnt work despite passing in the correct values?

    Whereas if I hardcode the values in the 2nd listing, it works.




    Listing One.
    Code:
    int PartialFind(char *f)
    {
     node *curr = NULL;
     curr = LastNode;
     while(curr)
     { 
      if(strstr(curr->IC,f)==NULL)
         return 0;
      else
      {
         printf("Found!");
         return 1;
      }
      curr = curr->Next;
     }
     return 0;
    }
    Listing Two:
    Code:
    int PartialFind(char *f)
    {
     node *curr = NULL;
     curr = LastNode;
     while(curr)
     { 
      if(strstr("HairyPotter","airy")==NULL)
         return 0;
      else
      {
         printf("Found!");
         return 1;
      }
      curr = curr->Next;
     }
     return 0;
    }
    If I'm able to turn back time, I would learn C as my first language.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Doubt it's a problem with strstr, do some debugging, check the contents of curr->IC before you pass it to strstr.

    Also, your code is rather weird, you have a while loop, but inside that loop you have an if statement that if true, returns from the function, and if false, returns from the function. What is the point of a loop in that case?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    Quote Originally Posted by cwr
    Doubt it's a problem with strstr, do some debugging, check the contents of curr->IC before you pass it to strstr.

    Also, your code is rather weird, you have a while loop, but inside that loop you have an if statement that if true, returns from the function, and if false, returns from the function. What is the point of a loop in that case?
    LOL. Silly me. Got it. I got what you meant.

    I'll try again.

    This looks better

    Code:
    int PartialFind(char *f)
    {
     node *curr = NULL;
     curr = LastNode;
     while(curr)
     { 
      if(strstr(curr->IC,f)!=NULL)
      {
         printf("Found!");
         return 1;
      }
      curr = curr->Next;
     }
     return 0;
    }
    Last edited by stevong; 11-27-2005 at 11:53 PM.
    If I'm able to turn back time, I would learn C as my first language.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    yes, that's a lot better. strstr() is case-sensitive, so "airy" is not the same as "Airy" or any other spelling. If you want case-insensitive comparisons then you have to convert the strings to all upper or lower case before calling strstr(). If you don't then strstr() will fail to find the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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