Page 1 of 4

px version of Ereplace()

Posted: Tue Jan 02, 2007 3:29 pm
by DSguru2B
Hi Dxsians,
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] ] )
The code that I prepared does everything except the "[ ,begin]" part. I developed this code on HP-UX. I ran and tested it out.

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
}
You can use it, tweak it, change it to fit your needs.
Any suggestions are welcomed.

Posted: Tue Jan 02, 2007 4:07 pm
by kumar_s
Awesome!!

Posted: Tue Jan 02, 2007 5:10 pm
by ray.wurlod
It might be better to copy the first argument into a local variable, so that the original string (first input argument) is unaffected (as is the case with the BASIC Ereplace() function).

Posted: Tue Jan 02, 2007 10:21 pm
by DSguru2B
Good suggestion. I am not exactly sure if its going to help but if I'll do that anyways, when i enhance it to handle the 'begin' part as well, sometime in the near future.

Posted: Wed Jan 03, 2007 10:50 am
by DSguru2B
Ok, i finally got the chance to complete it.

Code: Select all

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

char* pxEreplace(char *str, char *subStr, char *rep, int num, int beg)
{
  char *result = (char *)malloc (sizeof(char *));
  int newlen = strlen(rep);
  int oldlen = strlen(subStr);
  int i, x, count = 0;
  
  //If begining is less than or equal to 1 then default it to 1
  if (beg <= 1)
  {beg = 1;}

  //replace all instances if value of num less than or equal to 0
  if (num <= 0)
  {num = strlen(str);}

  //Get the character position in i for substring instance to start from
  for (i = 0; str[i] != '\0' ; i++)
   {
     if (strstr(&str[i], subStr) == &str[i])
     {
      count++;
      i += oldlen - 1;
      if (count == beg)
      {break;}
     }
   }

   //Get everything before position i before replacement begins

   x = 0;
   while (i != x)
   {  result[x++] = *str++; }

  //Start replacement
   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[x], rep);
          x += newlen;
          str += oldlen;
          num--;
       }
       else // if no match is found
       {
          result[x++] = *str++;
       }
    }
    else
    {
       result[x++] = *str++;
    }
   }

    result[x] = '\0'; //Terminate the string
    return result; //Return the replaced string
   
}
The only difference between this pxEreplace and Basice function Ereplace() is that pxEreplace requires all input variables.
EDIT:The variable that i added is beg
begspecifies the first instance to replace. A value less than 1 defaults to 1.

Posted: Wed Jan 03, 2007 10:55 am
by narasimha
Great job DSguru2B!

Posted: Wed Jan 03, 2007 11:44 am
by I_Server_Whale
Wonderful! :)

Posted: Wed Jan 03, 2007 12:29 pm
by us1aslam1us
Awesome!!!!

Posted: Thu Jan 25, 2007 8:46 pm
by kduke
Not 2B

I loaded this up and it works great.

Thanks Kim.

P.S. Send me an email if Dallas sounds okay.

Posted: Thu Jan 25, 2007 9:16 pm
by DSguru2B
kduke wrote:Not 2B

I loaded this up and it works great.

Thanks Kim.

P.S. Send me an email if Dallas sounds okay.
Me not Kim :wink:
I didnt get your P.S. Sorry my brains are dead right now.

Posted: Fri Jan 26, 2007 5:51 am
by kduke
You are looking for work in Texas. Is Dallas okay?

Posted: Fri Jan 26, 2007 7:29 am
by DSguru2B
Dallas sounds great. But currently I am engaged in a project in NJ. Ill let you know once I am done here. So you are going to make me your disciple :wink:

Posted: Fri Jan 26, 2007 7:43 am
by chulett
Slave.

Posted: Fri Jan 26, 2007 7:50 am
by DSguru2B
chulett wrote:Slave.
Maybe not.
Maybe a student.
On a serious note, I will definately let you know Kim once I am done with my current engagement. Thanks for looking out.
Regards,

Posted: Fri Jan 26, 2007 10:05 pm
by kduke
Harsh, Craig. Very harsh. He is NOT a 2B any more. He has arrived. No slave work for gurus.