Removing ocurrances of characters in a string

Posted by DmainEvent on Programmers See other posts from Programmers or by DmainEvent
Published on 2012-09-21T00:20:54Z Indexed on 2012/09/21 3:49 UTC
Read the original article Hit count: 185

Filed under:
|

I am reading this book, programming Interviews exposed by John Wiley and sons and in chapter 6 they are discussing removing all instances of characters in a src string using a removal string...

so removeChars(string str, string remove)

In there writeup they sey the steps to accomplish this are to have a boolean lookup array with all values initially set to false, then loop through each character in remove setting the corresponding value in the lookup array to true (note: this could also be a hash if the possible character set where huge like Unicode-16 or something like that or if str and remove are both relatively small... < 100 characters I suppose). You then iterate through the str with a source and destination index, copying each character only if its corresponding value in the lookup array is false...

Which makes sense... I don't understand the code that they use however... They have

for(src = 0; src < len; ++src){
   flags[r[src]] == true;
}

which is turning the flag value at the remove string indexed at src to true...

so if you start out with PLEASE HELP as your str and LEA as your remove you will be setting in your flag table at 0,1,2... t|t|t but after that you will get an out of bounds exception because r doesn't have have anything greater than 2 in it... even using there example you get an out of bounds exception... Am is there code example unworkable?

© Programmers or respective owner

Related posts about interview

Related posts about algorithms