Question: PROBLEM: search through a given string to find all instances of a forbidden word and replace it with a substitute word. I. Find all instances
PROBLEM: search through a given string to find all instances of a forbidden word and replace it with a substitute word. I. Find all instances of a forbidden word A. Locate all places in our string where the first letter of our forbidden word occurred. 1. Pair every char value in the string with a unique positive integer from 1 up to the length of the string 2. Use a mask to identify all locations of the first letter of our forbidden word and report that list of integers back to the user B. Determine which instances of that first letter also begin a word 1. If first letter in string, keep it 2. If not first letter in string, but is preceded by a space, keep it 3. Otherwise discard it 4. Return the modified list of integers representing only instances of first letter of forbidden word that ALSO begin a word to the user C. For each instance of first letter of forbidden word that also begins a word, scan until a space is reached: 1. If any letter in that part of string differs from the sequence of letters in our forbidden word, stop scan and discard location 2. Move to next location in list and begin scan again 3. Stop when all locations have been scanned, keeping only those locations where a space is reached without triggering a discard II. Replace forbidden word with substitute word A. Repeat for each instance of forbidden word beginning location given in the previous task 1. Select the char values to replace a) From forbidden word beginning location to a number of letters later in the string which match the length of the forbidden word 2. Actually make the replacement with a direct assignment statement
string=A quick fox is also quiet. forbidden=quiet substitute=*censored* Desired output: A quick fox is also *censored*. Test our algorithm from outline on previous page:
Step 0: add a space character to beginning of string (this will help simplify Steps 1-B-1-4 later) Step I-A-1: create the vector idx=1:length(string); (this will pair every char value in string with a unique positive integer Step 1-A-2: badlocs=idx(string==forbidden(1)); Steps 1-B-1-4: for each entry in badlocs, examine the entry before it in string; if the entry before is a space, keep the entry in badlocs if the letter before is not a space, discard the entry from badlocs. badlocs=[3,21] when testval = 3, check that string(3-1) is a spaceit is, so keep 3 in badlocs when testval = 21, check that string(21-1) is a spaceit is, so keep 21 in badlocs no more values to test Steps 1-C-1-3: for each remaining entry in badlocs, pair successive letters in string with successive letters in forbidden; if the letters differ before a space is reached, discard the locationif the letters dont differ and then a space is reached, keep the location. badlocs=[3,21] when scanval = 3, set keepgoing = true while keepgoing is true, compare letters string(3) is q, forbidden(1) is q they match, so continue string(4) is u, forbidden(2) is u they match, so continue string(5) is i, forbidden(3) is i they match, so continue string(6) is c, forbidden(6) is e no match, set keepgoing=false to exit while discard location from badlocs when scanval = 21, set keepgoing = true while keepgoing is true, compare letters string(21) is q, forbidden(1) is q they match, so continue string(22) is u, forbidden(2) is u they match, so continue string(23) is i, forbidden(3) is i they match, so continue string(24) is e, forbidden(4) is e they match so continue string(25) is t, forbidden(5) is t they match, but forbidden(5) is then end of forbidden, so stop scan and keep location in badlocs Step II-A: for each remaining entry in badlocs, replace string(badlocs(i):badlocs(i)+length(forbidden)-1) with substitute. badlocs =[21] newstring = string(1:badlocs(i-1)); newstring = [ newstring, substitute ]
Programming language is Matlab.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
