Strip a set of characters from a string: Difference between revisions
→{{header|C++}}: Added C++ solution |
|||
Line 25: | Line 25: | ||
{ |
{ |
||
std::cout << stripchars("She was a soul stripper. She took my heart!", "aei") << '\n'; |
std::cout << stripchars("She was a soul stripper. She took my heart!", "aei") << '\n'; |
||
return 0; |
|||
}</lang> |
}</lang> |
||
Output: |
Output: |
Revision as of 04:28, 10 June 2011
You are encouraged to solve this task according to the task description, using any language you may know.
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>
C++
Note: this uses a C++0x lambda function <lang cpp>#include <algorithm>
- include <iostream>
- include <string>
std::string stripchars(std::string str, const std::string &chars) {
str.erase( std::remove_if(str.begin(), str.end(), [&](char c){ return chars.find(c) != std::string::npos; }), str.end() ); return str;
}
int main() {
std::cout << stripchars("She was a soul stripper. She took my heart!", "aei") << '\n'; return 0;
}</lang> Output:
Sh ws soul strppr. Sh took my hrt!
D
<lang d>import std.stdio, std.string;
void main() {
auto s = "She was a soul stripper. She took my heart!"; auto ss = "Sh ws soul strppr. Sh took my hrt!"; assert(s.removechars("aei") == ss);
}</lang>
Haskell
I decided to make the string the second argument and the characters the first argument, because it is more likely for someone to partially apply the characters to be stripped (making a function that strips certain characters), than the string. <lang haskell>stripChars :: String -> String -> String stripChars = filter . flip notElem</lang>
testing in GHCI:
> stripChars "aei" "She was a soul stripper. She took my heart!" "Sh ws soul strppr. Sh took my hrt!"
Icon and Unicon
The following works in both languages: <lang unicon>procedure main(A)
cs := \A[1] | 'aei' # argument is set of characters to strip every write(stripChars(!&input, cs)) # strip all input lines
end
procedure stripChars(s,cs)
ns := "" s ? while ns ||:= (not pos(0), tab(upto(cs)|0)) do tab(many(cs))) return ns
end</lang>
Sample runs:
->strip She was a soul stripper. She took my heart! Sh ws soul strppr. Sh took my hrt! Aardvarks are ant eaters. Ardvrks r nt trs. ->strip AEIOUaeiou Aardvarks are ant eaters. rdvrks r nt trs. ->
J
Solution:
The dyadic primitive -.
(Less) is probably the simplest way to solve this task.
Example Usage: <lang j> '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 if String.contains cs s.[i] then aux (succ i) (j) else begin res.[j] <- s.[i]; aux (succ i) (succ j) end 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!"
Perl
Caveat: in this version hyphens in the second argument can be used to specify ranges; if you need to actually strip hyphens, you need to put a backslash before it <lang perl>sub stripchars {
my ($s, $chars) = @_; eval("\$s =~ tr/$chars//d;"); return $s;
}
print stripchars("She was a soul stripper. She took my heart!", "aei"), "\n";</lang> Output:
Sh ws soul strppr. Sh took my hrt!
Perl 6
<lang perl6>sub strip_chars ( $s, $chars ) {
return $s.trans( $chars.comb X=> );
}
say strip_chars( 'She was a soul stripper. She took my heart!', 'aei' );</lang>
Output:
Sh ws soul strppr. Sh took my hrt!
PHP
<lang php><?php function stripchars($s, $chars) {
return str_replace(str_split($chars), "", $s);
}
echo stripchars("She was a soul stripper. She took my heart!", "aei"), "\n"; ?></lang> Output:
Sh ws soul strppr. Sh took my hrt!
PicoLisp
<lang PicoLisp>(de strDiff (Str1 Str2)
(pack (diff (chop Str1) (chop Str2))) )</lang>
Output:
: (strDiff "She was a soul stripper. She took my heart!" "aei") -> "Sh ws soul strppr. Sh took my hrt!"
PureBasic
PureBasic uses a single (for ASCII) or a two-byte (for Unicode) null to signal the end of a string. Nulls are thus excluded from the allowable characters to strip as they can't be included in a PureBasic string. <lang PureBasic>Procedure.s stripChars(source.s, charsToStrip.s)
Protected i, *ptrChar.Character, length = Len(source), result.s *ptrChar = @source For i = 1 To length If Not FindString(charsToStrip, Chr(*ptrChar\c)) result + Chr(*ptrChar\c) EndIf *ptrChar + SizeOf(Character) Next ProcedureReturn result
EndProcedure
If OpenConsole()
PrintN(stripChars("She was a soul stripper. She took my heart!", "aei")) Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() CloseConsole()
EndIf</lang> Sample output:
Sh ws soul strppr. Sh took my hrt!
Python
<lang python>>>> def stripchars(s, chars): ... return s.translate(None, chars) ... >>> stripchars("She was a soul stripper. She took my heart!", "aei") 'Sh ws soul strppr. Sh took my hrt!'</lang>
<lang python>>>> import string >>> def stripchars(s, chars): ... return s.translate(string.maketrans("", ""), chars) ... >>> stripchars("She was a soul stripper. She took my heart!", "aei") 'Sh ws soul strppr. Sh took my hrt!'</lang>
Implemented manually: <lang python>>>> def stripchars(s, chars): ... return "".join(c for c in s if c not in chars) ... >>> stripchars("She was a soul stripper. She took my heart!", "aei") 'Sh ws soul strppr. Sh took my hrt!'</lang>
Ruby
<lang ruby>>> "She was a soul stripper. She took my heart!".delete("aei") => "Sh ws soul strppr. Sh took my hrt!"</lang>
Standard ML
<lang sml>fun stripchars (string, chars) = let
fun aux c = if String.isSubstring (str c) chars then "" else str c
in
String.translate aux string
end</lang>
testing in the interpreter:
- stripchars ("She was a soul stripper. She took my heart!", "aei") ; val it = "Sh ws soul strppr. Sh took my hrt!" : string
Tcl
<lang tcl>proc stripchars {str chars} {
foreach c [split $chars ""] {set str [string map [list $c ""] $str]} return $str
}
set s "She was a soul stripper. She took my heart!" puts [stripchars $s "aei"]</lang>