Re: Deleting substrings



goose wrote:

Ravi wrote:
A function which takes a string and a substring as input and which
deletes the substring in the main string.
Eg: mainstr=abcxyghixy sub=xy
Result should be mainstr=abcghi.

Will it require recursion ?

How to handle the deletion part..I mean deleting a substring and then
starting all over again looking for the next substring.


Well, you don't have to start all over again; you can always
start looking again at the point that you last stopped.

1. We start at the beginning of the string.
abcxyghixy
^

2. We scan from that point for "xy" and if we don't find it
then we are done, if we find it then we are at the following point
in the string:
abcxyghixy
^

3. We copy the characters from the point immediately after
"xy" into the current position:
abcghixy
^

4. We repeat steps 2 and 3 until step 2 tells us that we are done.

Not as a criticism of your solution (I'm just piggybacking), but
I wonder what the OP intends to do with

abcxxyydef

After `delete A from B`, is it allowed for the result to contain A?

[If not, then we just need to restart the scan not from where we
left off, but from a "suitable distance" before then.]

--
Chris "as many cases as corners" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

.



Relevant Pages

  • Re: Deleting substrings
    ... deletes the substring in the main string. ... How to handle the deletion part..I mean deleting a substring and then ... sequence 'xzy' should be removed. ...
    (comp.programming)
  • Re: Deleting substrings
    ... How to handle the deletion part..I mean deleting a substring and then ... We start at the beginning of the string. ... and simple machine suitable for a first state-machine. ...
    (comp.programming)
  • Re: Deleting substrings
    ... Will it require recursion? ... How to handle the deletion part..I mean deleting a substring and then ... I would assume that delete the substring means removes all instances of it, but removing all instances of a substring does not necessarily mean that the resulting string will not contain instances of it. ...
    (comp.programming)
  • Re: Deleting substrings
    ... deletes the substring in the main string. ... Will it require recursion? ... How to handle the deletion part..I mean deleting a substring and then ...
    (comp.programming)
  • Re: [QUIZ] Longest Repeated Substring (#153)
    ... My hack at the substring problem is based on suffix ... in the original string. ... def initialize ...
    (comp.lang.ruby)