Thread: !@#&*!!# strstr()

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    !@#&*!!# strstr()

    Hello everyone.

    Right now I am cursing the C language.

    Why does it have to be so buggy?

    Well here's my problem:

    I have an array of pointers. The pointers point to strings. Printing the array yields the proper output.

    I now want to SEARCH the array for certain strings.

    If a match occurs, print certain strings in the array.

    Simple, right?

    Well, not so simple, because when I search for KNOWN strings(or rather their pointers) in the array, sometimes it finds them, sometimes not! Yet it should every time because I've printed the arrays values, and everything's in it's place!!

    So why is "strstr()" being so picky?



    Let's look at the code.

    But first, let me show you a sample array printout:
    ___________________________________
    line_ptrs[0] = "john ";
    line_ptrs[1] = "peters ";
    line_ptrs[2] = "texas";
    line_ptrs[3] = "rick";
    line_ptrs[4] = "jones";
    line_ptrs[5] = "washington";
    line_ptrs[6] = "sarah";
    line_ptrs[7] = "doyle";
    line_ptrs[8] = "texas";
    line_ptrs[9] = "sally";
    line_ptrs[10] = "ericson";
    line_ptrs[11] = "washington";
    .....etc.
    ___________________________________

    Now for the code:
    //////////////////////////// SNIPPET OF CODE ///////////////////
    PHP Code:
    char state[20];

    int q=0;

    int y;

    printf("Search for all people in a certain State...\"1\" exits...");

    fgets(state,19,stdin);

    while( (
    strcmp(state,"\n")) && (q=atoi(state) != 1))
      { 
    /// the above means "while user hasn't entered a "1" or a newline..." 

       
    for(y=0;y<max_num;y++)  ///"max_num" defined elsewhere
               
    {
                   if( 
    strstr(line_ptrs[y],state) )
                    {
                      
    printf("%s\n",line_ptrs[y-2]); //print first name
                      
    printf("%s\n",line_ptrs[y-1]);  //print last name
                    
    }
                }

    printf("Search for all people in a certain State...\"1\" exits...");

    fgets(state,19,stdin);
      } 
    ///////////////////////// END SNIPPET///////////////////////////////

    So even if ten of the pointers are "texas" only three successful "strstr()" attempts might work!

    Or if there are eight "washington"s, strstr() might find seven!!

    AAAAAAAARGH!

    This inconsistancy shows thoughout the execution of this routine.

    Any ideas?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I've run into problems of the same nature before. SInce I don't have too much time my only advice is not to use strstr() it is weird and has little practical use.

  3. #3
    zoo
    Guest
    You need a:
    state[strlen(state)-1] = '\0';
    just inside the while loop to remove the newline character.

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ...and strstr() is used to search for sets of characters within a string as you are comparing whole strings with whole strings you could have used strcmp().

  5. #5
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Angry ugh

    I hate when people think C is @ fault when they have bad code... and also strstr is not a part of C... it is jsut a part of the string lib. and also ststr is for a string... noto an array of poitners to strings... think, its not telepathic, how does it know that these are pointers to strings, where does it say "if this function is givin a string of strings, it will add them all together and scan that". No, it will just look for patterns in the pointers, not wut they are pointing to.

    No Offence, SPH

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You need a:
    state[strlen(state)-1] = '\0';
    just inside the while loop to remove the newline character.
    Hmm, I'll try it but where exactly in the code?

    Besides, it looks like you are simply replacing the char before the newline with a newline, instead of removing the newline from the string...

    Anyway, are there any replacements for "strstr()"?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    ugh
    I hate when people think C is @ fault when they have bad code... and also strstr is not a part of C... it is jsut a part of the string lib. and also ststr is for a string... noto an array of poitners to strings... think, its not telepathic, how does it know that these are pointers to strings, where does it say "if this function is givin a string of strings, it will add them all together and scan that". No, it will just look for patterns in the pointers, not wut they are pointing to.

    No Offence, SPH
    Actually, the results were precisely the same whether I used the array of pointers or the array of arrays of chars(the actual table of strings). And since it did work some of the time, I figured there was just some subtlety I wasn't getting. BTW, I am not strstr()ing the whole damn thing at once! NOTICE the for() loop. One at a time. O.K.?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    zoo
    Guest
    Like this:
    PHP:--------------------------------------------------------------------------------
    char state[20];

    int q=0;

    int y;

    printf("Search for all people in a certain State...\"1\" exits...");

    fgets(state,19,stdin);

    while( (strcmp(state,"\n")) && (q=atoi(state) != 1))
    {
    /// the above means "while user hasn't entered a "1" or a newline..."

    state[strlen(state)-1] = '\0';

    for(y=0;y<max_num;y++) ///"max_num" defined elsewhere
    {
    if( strstr(line_ptrs[y],state) )
    {
    printf("%s\n",line_ptrs[y-2]); //print first name
    printf("%s\n",line_ptrs[y-1]); //print last name
    }
    }

    printf("Search for all people in a certain State...\"1\" exits...");

    fgets(state,19,stdin);
    }--------------------------------------------------------------------------------

  9. #9
    Registered User Xaviar Khan's Avatar
    Join Date
    Sep 2001
    Posts
    10
    Personally, I think an strcmp() would be more efficient anyway
    saves headaches sometimes, you should try it.... he's right, you're matching -exact- strings, why do something that you don't needa do Besides, it wouldn't be too hard to change

    And, state[strlen(state)-1] = '\0'; is old... heheh use..
    state[strlen(state)-1] = NULL; even though it's likely noone
    cares :O

    Sue me, I'm picky :P
    Xaviar Khan
    Programmer/Owner
    Shards of Destiny (SoD)
    ---------------------------------------
    Reach us with any MUD client at ShardsofDestiny.org
    port 9000.
    +------------------------------------+
    | Html/Java support added, |
    | 'www.ShardsofDestiny.org' |
    +------------------------------------+

  10. #10
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Your making it too confusing with mixed data in the array.
    At one point

    Code:
    for(y=0;y<max_num;y++)  ///"max_num" defined elsewhere
               {
                   if( strstr(line_ptrs[y],state) )
                    {
                      printf("%s\n",line_ptrs[y-2]); //print first name
                      printf("%s\n",line_ptrs[y-1]);  //print last name
                    }
                }
    You ending up comparing first names and last names to states,
    I'm not sure why you don't use strcmp though?

    Code:
    for (y = 2; y < max_num; y += 3)
    but this is is probably making the code look like a mess.
    I would do this

    Code:
    struct Person {
        char* first_name;
        char* last_name;
        char* state;
    };
    typedef Person Person;
    
    Person person[max_num] = { {"John", "Peters", "Washington"}
                                             };

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Talking

    Thanks Zoo! It works like a charm!

    And as far as "strcmp()" is concerned guys, I will be adding data to each string, so "strstr()" type functions is what I'm looking for.

    I simplified the problem to be more readable, and I confused a lot of people in the process!

    But all is well now, thanks to all of your help. I can now go to sleep! Goodnight...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 09:32 PM
  3. linked list using strstr
    By ilovec.. in forum C Programming
    Replies: 3
    Last Post: 11-04-2006, 01:30 PM
  4. strstr on a wchar array
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-28-2006, 06:42 AM
  5. Question about strstr()
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2004, 08:18 PM