cpix
asked on
Replace a string, in a string function
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.
to create a
char *strreplace(oRgLine, srcWord, newWord )
which will replace all occurences srcword with newword in orgline.
ASKER
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).
ASKER
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).
ASKER
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(newst ring) );
strcat(resString2, nyforekomst);
strcat(resString2, resString);
memset(inStr,0,strlength);
memset(resString,0,strleng th);
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.
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(newst
strcat(resString2, nyforekomst);
strcat(resString2, resString);
memset(inStr,0,strlength);
memset(resString,0,strleng
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Just to end the thread :)
Do you need the general case? Or are there some limitations/assumptions/sp