Start New Discussion within our Software Development Community

Hi,
I'm a beginner.
Could anybody help me out with this.
How to make a class strin subtraction operator overload, that if it finds "Bill", then deletes it from the text string.
Thank you in advance.

#include <iostream.h>
#include <string.h>

class strin {
   char *place; 
   int length; 
public:
   strin (char *text); 
   strin (); 
   strin (strin &kit); 
   ~strin () { delete[] place; } 
   int getlen () { return length; } 
   strin & operator + (strin &arg); 
   strin & operator = (const strin &arg);
   void show() { cout << place << endl; }
};

main() {
   strin a ("The quick pretty brown cow jumps over Bill"),
   b ("Bill"), c;
   a.show(); 
   c = a + b; 
   c.show(); 
   a.show();
   strin d(b); 
   c.show(); 
}

strin :: strin (char *text) { 
   length = strlen (text); 
   place = new char[length+1]; 
   strcpy(place, text); 
}

strin :: strin() { 
   length = 0;
   place = new char[1];
   *place = '\0'; 
}

strin :: strin (strin &kit) { 
   length = kit.length; 
   place = new char[length+1];
   strcpy(place, kit.place);
}

strin & strin :: operator + (strin &arg) { 
   strin *temp = new strin; 
   temp -> length = length + arg.length; 
   temp -> place = new char[temp->length+1]; 
   strcpy (temp->place, place); 
   strcat (temp->place, arg.place); 
   return *temp; 
}

strin & strin :: operator = (const strin &arg) {
   if (this != &arg) { 
      delete [] place; 
      length = arg.length; 
      place = new char[length+1]; 
      strcpy (place, arg.place); 
  }
return *this; 
}

Use strstr to find the string you want to remove and then copy over it. Someting along these lines:

char *p = strstr ( place, arg );
int start = p - place;
int end = start + strlen ( arg );

memmove ( place + start, place + end, strlen ( place + end ) + 1 );
This article has been dead for over six months. Start a new discussion instead.