How to resolve the algorithm Strip a set of characters from a string step by step in the REXX programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Strip a set of characters from a string step by step in the REXX programming language
Table of Contents
Problem Statement
Create a function that strips a set of characters from a string.
The function should take two arguments:
The returned string should contain the first string, stripped of any characters in the second argument:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Strip a set of characters from a string step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program removes a list of characters from a string (the haystack). */
say stripChars('She was a soul stripper. She took my heart!', "iea") /*elide: iea */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
stripChars: procedure; parse arg haystack, remove
do j=1 for length(remove)
haystack=changestr( substr( remove, j, 1), haystack, '')
end /*j*/
return haystack
/* REXX */
say StripChars('She was a soul stripper. She took my heart!','iea')
exit 0
StripChars: procedure
parse arg strng,remove
removepos=Verify(strng,remove,'MATCH')
if removepos=0 then return strng
parse value strng with strng =(removepos) +1 rest
return strng || StripChars(rest,remove)
/* REXX ***************************************************************
* If source and stripchars don't contain a hex 00 character, this works
* 06.07.2012 Walter Pachl
* 19.06.2013 -"- space(result,0) -> space(result,0,' ')
* space(result,0) removes WHITESPACE not only blanks
**********************************************************************/
Say 'Sh ws soul strppr. Sh took my hrt! -- expected'
Say stripchars("She was a soul stripper. She took my heart!","aei")
Exit
stripchars: Parse Arg string,stripchars
result=translate(string,'00'x,' ') /* turn blanks into '00'x */
result=translate(result,' ',stripchars) /* turn stripchars into ' ' */
result=space(result,0,' ') /* remove all blanks */
Return translate(result,' ','00'x) /* '00'x back to blanks */
stripchars: Procedure
Parse Arg i,s /* get input and chars to be removed */
o='' /* initialize result */
Do While i\=='' /* loop through input */
Parse Var i c +1 i /* get one character */
If pos(c,s)=0 Then /* it's not to be removed */
o=o||c /* append it to the result */
End
Return o /* return the result */
You may also check:How to resolve the algorithm Proper divisors step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Raku programming language
You may also check:How to resolve the algorithm Ramanujan's constant step by step in the Julia programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Haskell programming language
You may also check:How to resolve the algorithm Soloway's recurring rainfall step by step in the ALGOL 68 programming language