Thread: Why doesn't this work? while ( searchPtr = strstr( searchPtr + 1, searchString ) )

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Why doesn't this work? while ( searchPtr = strstr( searchPtr + 1, searchString ) )

    The if structure is similar and seems to work just fine. The program completely skips over the while structure though.

    Is this an appropriate way to use conditional structures? I imagine this could be considered poorly readable code but I wouldn't know.

    Code:
    /*
    8.16
    Write a program that inputs a line of text and a search string from the 
    keyboard.  Using function strstr, locate the first occurrence of the search 
    string in the line of text, and assign the location to variable searchPtr of
    type char *.  If the search string is found, print the remainder of the line of
    text beginning with the search string.  Then, use strstr again to locate the 
    next occurrence of the search strig in the line of text.  If a second occurrene
    is found, print the remainder of the line of text begiining with the seond 
    occurrence.  (Hint: The second call to strstr should contain searchPtr + 1 as 
    its first argument. )
    */
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char *searchPtr;
      char bulk[ 100 ] = { };
      char searchString[ 100 ] = { };
    
      printf( "Enter the initial string: " );
      scanf( "&#37;s", bulk );
    
      printf( "Enter string to search for: " );
      scanf( "%s", searchString );
    
    
      if ( searchPtr = strstr( bulk, searchString ) ) {
        printf( "%s\n", searchPtr );
        
        while ( searchPtr = strstr( searchPtr + 1, searchString ) ) {
          printf( "%s\n", searchPtr );
        }
        
      }
    
      // Test commands below to pinpoint problem
      //searchPtr = strstr( searchPtr + 1, searchString );
      //printf( "%s\n", searchPtr );
    
      return 0;
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Edit: blegh ignore me again; im confused o_0
    Last edited by mike_g; 03-09-2008 at 05:00 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're not expecting scanf to read in a string with spaces in it, are you?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    182
    No, I've been using stuff like "abcdefsomethingdefhellodefdef

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by yougene View Post
    No, I've been using stuff like "abcdefsomethingdefhellodefdef
    Then it works. (No really, I just compiled it and tried it.)

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Looks good to me. The while part would logically be skipped if the searchString is only found once in bulk, but i would swear it will execute if it is found more than once.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Works for me too. You might need some getchars to keep the window open?

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    182
    I should have included my commandline output

    Apparently if there are alot of matches it'll find some of them but not all.
    Code:
    root[~]# ./a.out
    Enter the initial string: abcdefsomethingdefhellodefdef
    Enter string to search for: def
    defsomethingdefhellodefdef
    defhellodefdef
    root[~]#

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I still get
    Code:
    silverlx:~$ ./a.out
    Enter the initial string: abcdefsomethingdefhellodefdef
    Enter string to search for: def
    defsomethingdefhellodefdef
    defhellodefdef
    defdef
    def
    Are you sure you don't need to recompile or something? Because the source code you've shown is right.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    182
    Hmm, for some reason recompiling did work. Maybe I didn't get emacs to save last time I ran it. : \

    I apologize for the brainfart.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strstr and strtod .. cant work together
    By galaxy_virus in forum C Programming
    Replies: 9
    Last Post: 10-03-2008, 02:13 AM
  2. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 09:32 PM
  3. strstr() - Not able to get it to work.
    By stevong in forum C Programming
    Replies: 3
    Last Post: 11-28-2005, 04:37 AM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM