I wrote a px version of Ereplace(). It is not complete. I would say it is about 80% complete.
The following syntax is directly out of DS help
Code: Select all
Ereplace (string, substring, replacement [ ,number [ ,begin] ] )
It has four input variables.
str is the string or expression.
subStr is the substring you want to replace.
rep is the replacement substring
num specifies the number of instances of substring to replace. To change all instances, use a value of 0.
Code: Select all
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
char* pxEreplace(char *str, char *subStr, char *rep, int num)
{
char *result = (char *)malloc (sizeof(char *));
int newlen = strlen(rep);
int oldlen = strlen(subStr);
int i = 0;
//replace all instances if value of num less than or equal to 0
if (num <= 0)
{num = strlen(str);}
while (*str) //for the complete input string
{
if (num != 0 ) // untill no more occurances need to be changed
{
if (strstr(str, subStr) == str )
{
strcpy(&result[i], rep);
i += newlen;
str += oldlen;
num--;
}
else // if no match is found
{
result[i++] = *str++;
}
}
else
{
result[i++] = *str++;
}
}
result[i] = '\0'; //Terminate the string
return result; //Return the replaced string
free(result); //free memory
}
Any suggestions are welcomed.