Strip a set of characters from a string: Difference between revisions
Content added Content deleted
(added ocaml) |
|||
Line 4: | Line 4: | ||
<lang pseudocode> print stripchars("She was a soul stripper. She took my heart!","aei") |
<lang pseudocode> print stripchars("She was a soul stripper. She took my heart!","aei") |
||
Sh ws soul strppr. Sh took my hrt!</lang> |
Sh ws soul strppr. Sh took my hrt!</lang> |
||
=={{header|OCaml}}== |
|||
<lang ocaml>let stripchars s cs = |
|||
let len = String.length s in |
|||
let res = String.create len in |
|||
let rec aux i j = |
|||
if i >= len then String.sub res 0 j |
|||
else match String.contains cs s.[i] with |
|||
| true -> |
|||
aux (succ i) (j) |
|||
| false -> |
|||
res.[j] <- s.[i]; |
|||
aux (succ i) (succ j) |
|||
in |
|||
aux 0 0</lang> |
|||
testing in the toplevel: |
|||
<pre># stripchars "She was a soul stripper. She took my heart!" "aei" ;; |
|||
- : string = "Sh ws soul strppr. Sh took my hrt!"</pre> |
|||
[[Category:String manipulation]] |
[[Category:String manipulation]] |
Revision as of 17:51, 5 June 2011
Strip a set of characters from a string is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
The task is to create a function that strips a set of characters from a string. The function should take two arguments: the first argument being a string to stripped and the second, a string containing the set of characters to be stripped. The returned string should contain the first string, stripped of any characters in the second argument:
<lang pseudocode> print stripchars("She was a soul stripper. She took my heart!","aei") Sh ws soul strppr. Sh took my hrt!</lang>
OCaml
<lang ocaml>let stripchars s cs =
let len = String.length s in let res = String.create len in let rec aux i j = if i >= len then String.sub res 0 j else match String.contains cs s.[i] with | true -> aux (succ i) (j) | false -> res.[j] <- s.[i]; aux (succ i) (succ j) in aux 0 0</lang>
testing in the toplevel:
# stripchars "She was a soul stripper. She took my heart!" "aei" ;; - : string = "Sh ws soul strppr. Sh took my hrt!"