Thread: Very easy issue with strstr

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    12

    Very easy issue with strstr

    Hi I have a piece of code that's supposed to find and change all instances of words in a sentence. Here's what it pretty much looks like:

    Code:
    while(replace = strstr (p, from)){ 
    		while(*to){
    			*replace = *to;
    			replace++;
    			to++;
    		}
    	}
    Anyways, replace, to, p, and from are all pointers.

    p points to an array containing the original sentence: i.e. "that guy is a great guy"
    from points to the word to be changed: i.e. "guy"
    to points to the word that from is changed to: i.e. "girl"
    replace just points to what strstr returns (as shown)

    Now for some reason my program is infinite looping. I assume because replace keeps being pointed to the first instance. I have used a print statement to check and receive:

    "that girl is a great guy"

    repetitively when using that as my test sentence.

    How can I fix this issue?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    From the looks of it, you aren't resetting 'to' to point back to the start of the string ("girl") each time. After it replaces the first occurrence, it will still be pointing towards the terminating NUL character and so the second, third, etc occurrences won't be replaced.
    Last edited by DeadPlanet; 12-19-2010 at 04:27 AM. Reason: Clarity

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Since "girl" is longer than "guy", I don't believe your straight replacement logic will work correctly.

    This works for a straight word replacement where the words are equal length, but replace "dog" with "girl" and now the space before "is" will be overwritten.

    Code:
     #include <string.h> 
     #include <stdio.h>
    
     int main(void)  {
       char s[] = "that guy is a great guy";
       char *dest = s;
       char *from = "guy";
       char *to = "dog";
       char *c=NULL;
       printf("destination prior to memmove: %s\n", dest);
       while(c = strstr(dest, from)) {
         if(c)
           memmove(c, to, 3);
         dest++;
       }
       printf("     source after memmove:    %s\n", s);  
       
       printf("\n\n\t\t\t     press enter when ready");
       (void) getchar(); 
       return 0;
     }
    I think the logic you need is:
    Code:
    while strstr() finds another instance of *to
      take each char before the pointer that strstr() returns, and put them into another   
      string (a much longer string). I believe strcat() will work ok, even on an empty string, 
      but check that.
    
      strcat the to, onto this string
      increment the pointer, same as above
    loop back for another strstr() address, if there is one
    What do you think?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This seems like a straight forward way to handle replacing words of unequal length.

    Code:
    /* Replaces a "from" word, with the "to" word, in a string of char's.
       
    status: OK
     
    */
    #include <string.h> 
    #include <stdio.h>
    
    int main(void)  {
      char s[] = "that guy is a great guy";
      char s1[100]={"\0"};
      int i,j, length_from, length_to;
      char *from = "guy";
      char *to = "elephant";
      char *c=NULL;
      printf("\nOriginal char string: %s\n", s);
      length_from=strlen(from);
      length_to=strlen(to);
      i=j=0;
      while(c = strstr(&s[i], from)) {  //i is the iterator for s 
        while(&s[i]!= c)                //j is the iterator for s1
        s1[j++]=s[i++];
        strcat(s1, to);
        j+= length_to;       //adjusting index by the length of the to word
        i+= length_from;   //repeating, by the length of the from word
        //printf("\ns1: %s", s1);
      }
      printf("          New string: %s\n", s1);  
       
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar(); 
      return 0;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
      while(c = strstr(&s[i], from)) {  //i is the iterator for s 
        while(&s[i]!= c)                //j is the iterator for s1
        s1[j++]=s[i++];
    Your indentation could use a little work Adak.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That was my test to see if ya'll were paying attention.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    That was my test to see if ya'll were paying attention.
    LOL... Don't ya just hate it when that happens?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Naw, like the song says:

    "Life will never be, exactly like you want it to be..."
    ... Mamas and the Papas

    Just had a great laugh, as reported on the BBC. UK squad under fire in Afghanistan, see's the 3 gunmen on a rooftop, and starts returning fire. Two of the gunmen take off, the last one grabs a young girl for a shield, and keeps on firing. The Brits don't shoot back, fearing to hit the girl, then one Brit solder yells out:

    "I've been shot in the head, but I'm fine!"

    The gunman's bullet hit, but did not get through, his helmet.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have moved some posts in this thread to Pascal and Indentation (for lack of a better name).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with strstr func...embedded system
    By kievari in forum C Programming
    Replies: 2
    Last Post: 03-27-2010, 03:43 AM
  2. Some weird issue.
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 04-12-2009, 04:10 PM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  4. re-entrancy pattern issue setbacks
    By George2 in forum Windows Programming
    Replies: 0
    Last Post: 04-12-2008, 02:23 AM
  5. How can I issue AT commands in VC++ ?
    By Cube in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-10-2001, 07:45 AM