Thursday, April 19, 2007

 

Substituting One Pattern for Another (Ch. 11)

Let's say you want to change the string "music" to "muzic".

The method Turing uses is to separate the first part, to just before the "s" for which you want to substitute a "z", add the "z", then add the rest of the string that followed the "s". In other words, like this:


You get the first part, "mu", by taking the string from postion 1 until index (string, pattern) minus 1. Again, as we did in the previous exercise, let's simplify by creating the variable pos : int := index (string, pattern). So separating the "mu" of "music" looks like this:

word (1 .. pos - 1

We add the "z" like this:

+ "z"

and we finish by adding the part of the string AFTER the "s" like this:

pos + 1 .. *


In summary, the complete line to substitute the "s" in "music" for a "z" is

word: = word ((1 .. pos -1) + "z" + (pos +1 .. *))


What do you do with the pieces of string!



<< Home

This page is powered by Blogger. Isn't yours?