Removing substring

J

Jack

I am fairly new to c++. I am trying to write a function that removes
all occurrences of one string from another without using any C library
functions.
So here is the function prototype:
void RemoveString(char * SourceAndDest, const char * ToRemove);

Sample input: "Boris Yeltsin is gone.", "is"
Sample output: "Bor Yeltsin gone."

Please note that the first char pointer is the source and the
DESTINATION.

the Any ideas.
 
M

michael.goossens

2 loops

First take the first character from ToRemove and let the first loop go
over your source. If you encounter the same character in the source
start the second loop. This loop moves the source and toremove forward
as long as they are equal and toremove has more content. If it runs
out of content than you have found yourself a match. Otherwise go back
to your first equal character and go further from there.
 
J

Jack

2 loops

First take the first character from ToRemove and let the first loop go
over your source. If you encounter the same character in the source
start the second loop. This loop moves the source and toremove forward
as long as they are equal and toremove has more content. If it runs
out of content than you have found yourself a match. Otherwise go back
to your first equal character and go further from there.

That part I get. The issue is with actually deleting the ToRemove from
the Source Dest. i am wondering if there is an efficient way of doing
this.
 
M

michael.goossens

That part I get. The issue is with actually deleting the ToRemove from
the Source Dest. i am wondering if there is an efficient way of doing
this.

I don't think you can do it efficiently with an array, moving the rest
of the string forward is the only option. You could use dynamic data
structures, like a vector or arraylist then the removal takes 1 line
of code.
 
E

Erik Wikström

That part I get. The issue is with actually deleting the ToRemove from
the Source Dest. i am wondering if there is an efficient way of doing
this.

You should probably instead create a new string and instead copy the
parts that you want to keep.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top