Re: Deleting substrings



On 28 Jun 2006 13:43:56 -0700, "Ravi" <ravib.1981@xxxxxxxxx> 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.
An alternative without recursion would be to use a state machine to
copy the characters you want from the source string to the destination
string. Something like:
[Monospaced font recommended]

State Character Actions New State
======= ========= ============ =========
Copying 'x' step 1 x-found

Copying \0 copy \0 Finish

Copying ~'x', ~\0 copy current Copying
step 1

x-found 'y' step 1 Copying

x-found \0 copy 'x' Finish
copy \0

x-found ~'y', ~\0 copy 'x' Copying
copy current
step 1

Finish any return

I have assumed C-style strings with a \0 terminator. The step 1
action moves the source index forward one. The copy action copies the
character to the destination and increases the destination index by
one. "copy current" copies whatever character is currently at the
source index and increases the destination index by one. The two
logical strings can be the same piece of memory as the destination
cannot be longer than the source.
.



Relevant Pages

  • 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)
  • Favicon type detection - possible?
    ... this is my cobbled together browser detection ... {string: navigator.userAgent, ... subString: "OmniWeb", ... {// for newer Netscapes ...
    (comp.lang.javascript)
  • Comments on Comments (was Re: Getting to 100 (#119))
    ... # yield each partitioning of the receiver into count partitions ... # an initial substring of increasing length, ... # the string into count-1 partitions. ... #:ops - an array of strings representing the operators to be inserted into ...
    (comp.lang.ruby)
  • RE: MAPI problem with HrGetServerDN
    ... 'Destination Host Unreachable ... Private Declare Function WSACleanup Lib "WSOCK32.DLL" As Long ... 'Create a handle on which Internet Control Message Protocol (ICMP) requests ... Private Declare Function inet_addr Lib "WSOCK32.DLL" (ByVal cp As String) As ...
    (microsoft.public.exchange.applications)
  • Re: Computability and logic
    ... as regards a mathematical formulation of a substring ... substring in a string with another substring? ... result of substituting a term for a variable in a formula'. ... As I recall, he doesn't get into replacing term for term, ...
    (sci.logic)