Replace a string, in a string function

cpix
cpix used Ask the Experts™
on
Im wondering if anyone could please show me how
to create a

char *strreplace(oRgLine, srcWord, newWord )
which will replace all occurences srcword with newword in orgline.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
HMmmm. The general case is probably going to be quite messy. If you have to deal with a newWord that can be more characters than the srcWord, then you would either have to guarantee that oRgLine "has" sufficient memory to contain the new expanded string (and relying on the caller to do that is not ideal), or you have allocate a buffer to hold the new string (and then you have to rely on the caller to free it; also not ideal).

Do you need the general case? Or are there some limitations/assumptions/specifications about what parameters the caller will be handing in.

Author

Commented:
the newWord is fixed (fixed length), so are the srcWord, and in this case it's simple to calculate how many chars that are added to the string, since I know how many words it needs to change. (even though it wont be the most ideal way to do it).

Author

Commented:
the newWord is fixed (fixed length), so are the srcWord, and in this case it's simple to calculate how many chars that are added to the string, since I know how many words it needs to change. (even though it wont be the most ideal way to do it).

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Ok, I got it working, but for everyone else who wants to know how to do this, this is it :-)


char *ReplaceString(char *innString, char *forekomst, char *nyforekomst, const int strlength)
{
  char *resString;
  char *resString2;

  char *newstring;
  char *inStr;

  inStr = (char *)malloc(strlength);
  newstring = (char *)malloc(strlength);
  resString = (char *)malloc(strlength);
  resString2 = (char *)malloc(strlength);
  strcpy(inStr, innString);
  while (1)
    {
      if (strstr(inStr, forekomst) != NULL)
        {
          strcpy(newstring, strstr(inStr,forekomst));
          strcpy(resString, &newstring[7]);
          strncpy(resString2, inStr, strlen(inStr)-strlen(newstring) );

          strcat(resString2, nyforekomst);
          strcat(resString2, resString);


          memset(inStr,0,strlength);
          memset(resString,0,strlength);
          memset(newstring, 0, strlength);
          memcpy(inStr, resString2, strlength);
          memset(resString2,0, strlength);

        }else
          {
            break;
          }
    }
 
  return inStr;
  free(inStr);
  free(resString);
  free(resString2);
  free(newstring);
}

-- cPix.
Commented:
if u want to just change the size of original string .. to get more space for new characters .. then u can use realloc also ..
but there is one problem with that .. while replacing onld word with new word.. u'll have to shift the other trailing words back /forward .. depending on the relative size of old and new words.

and which is not that bad .. if u notice .. when u work in editors . when u replace words .. how rest of the text re-aligns without much fuss

Author

Commented:
Just to end the thread :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial