const char *Str_Replace(const char *c, const char *sub, const char *s3)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LuxOcculta
    New Member
    • Nov 2009
    • 7

    const char *Str_Replace(const char *c, const char *sub, const char *s3)

    Hello, I need some help..

    I have to replace in s1, the string s2 with the string s3. I first found if s2 was a substring in s1, but i dont know what to do from here, I'm lost..

    example: s1 = "It is a good day to die", s2 = "die", s3 = "pay up"
    => after execution s1 will be "it is a good day to pay up"

    Here's the code for the function:

    Code:
    const char *String_Replace(const char *c, const char *sub, const char *s3)
    {
          if(!*sub)
              return c;
          for (;*c;c++)
          {
              if(*c==*sub)
              {
                   const char *h,*n;
                   for(h=c,n=sub;*h&&*n;h++,n++)
                   {            
                        if(*h!=*n)
                             break;
                   }
                   if(!*n)
                        return c;                   
              }
          }
          return 0;
    }
    please help, I'm lacking ideas..
    Thanks
    Last edited by Banfa; Nov 11 '09, 10:01 AM. Reason: Added [Code] ... [/code] tags
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    You need to create the output string -- you can't merely alter input string s1. Why do I say that? The first reason is that the s1 parameter is declared as a const char *, which means that the content of the string can't be changed. The second reason is that the output string can be longer than the input string -- you can't count on the memory allocation for s1 to be big enough to hold the longer string.

    Do you need to replace the first occurrence of s2 or all occurrences? If you only need to replace the first occurrence then you can quickly determine how much memory to allocate for the output string: strlen(s1) + strlen(s3) + 1. This is actually a little more memory than you need, but I wouldn't worry about it.

    By the way, your code is not consistent with your text. Are the function arguments s1, s2, s3 or are they c, sub, s3?

    Comment

    • LuxOcculta
      New Member
      • Nov 2009
      • 7

      #3
      the arguments are c, sub, and s3, and i have to change for every occurrance.. and i must not use string.h functions :|

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        OK ... I assume that c is the same as s1, and sub is the same as s2.

        Don't worry about <string.h> ... it is trivial to roll your own version of strlen, as long as you give it a different name.

        What's the answer to the question whether you are supposed to replace all instances of sub/s2 or only the first?

        Comment

        • LuxOcculta
          New Member
          • Nov 2009
          • 7

          #5
          replace for all instances.. is the function helping in any way ?

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Copy from s1 to the output string until you find an instance of s2.
            Then copy all of s3 into the output string.
            Then resume copying from s1 after the end of the embedded instance of s2.
            Repeat these steps until you reach the end of s1.

            You need to be careful that you don't write past the end of the output string allocation. You can do this either by allocating a big enough output string or by increasing the allocation as needed.

            Next question about the requirements: does this function need to support recursive replacements? That is, if new instances of s2 are constructed by pasting in s3 then do those new instances also need to be replaced? If so, then how do you protect yourself from infinite loop when s3 itself contains an instance of s2?

            Comment

            • LuxOcculta
              New Member
              • Nov 2009
              • 7

              #7
              no, it didnt say anything about recursive replacement..

              Comment

              • LuxOcculta
                New Member
                • Nov 2009
                • 7

                #8
                offtopic: Do you know any books/tutorials to help me understand better:
                (expr) ? val1 : val2 ;
                are there more of these ?

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by LuxOcculta
                  offtopic: Do you know any books/tutorials to help me understand better:
                  (expr) ? val1 : val2 ;
                  are there more of these ?
                  See the conditional / ternary operator.

                  Comment

                  • LuxOcculta
                    New Member
                    • Nov 2009
                    • 7

                    #10
                    i did this so far... but it isnt working...my brain wont work with me today...

                    Code:
                    char *String_Replace(char *c,char *sub,char *s3)
                    {
                         char *u;
                         if(!*sub)
                             return 0;
                         for (;*c;c++)
                         {
                             if(*c==*sub)
                             {
                                  const char *h,*n,*m;
                                  for(h=c,n=sub;*h&&*n;h++,n++)
                                  {            
                                       if(*h!=*n)
                                            break;
                                  }
                                  if(!*n)
                                  {
                                         while(!*m)
                                         {
                                             *u=*m;
                                             cout<<*u;
                                             u++;
                                             m++;
                                         }                    
                                  }                                    
                             }
                             *u=*c;
                             u++;
                         }
                         return u;
                    }

                    Comment

                    • LuxOcculta
                      New Member
                      • Nov 2009
                      • 7

                      #11
                      this is the latest one... where am i doing it wrong?

                      Code:
                      char *String_Replace(char *c,char *sub,char *s3)
                      {
                           if(!*sub)
                               return c;
                           for (;*c;c++,u++)
                           {
                               if(*c==*sub)
                               {
                                    const char *h,*n,*m;
                                    m=s3;
                                    for(h=c,n=sub;*h&&*n;h++,n++)
                                    {            
                                         if(*h!=*n)
                                              break;
                                    }
                                    if(!*n)
                                    {
                                        for(;*m;m++,u++)
                                            *u=*m;         
                                    }                                    
                               }
                               *u=*c;
                           }
                           return u;
                      }

                      Comment

                      Working...