Tokenize a string: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 1:
{{task|String manipulation}}
Separate the string "Hello,How,Are,You,Today" by commas into an array (or list) so that each element of it stores a different word. Display the words to the 'user', in the simplest manner possible, separated by a period. To simplify, you may display a trailing period.
 
=={{header|ACL2}}==
<lang lisp>(defun split-at (xs delim)
(if (or (endp xs) (eql (first xs) delim))
(mv nil (rest xs))
(mv-let (before after)
(split-at (rest xs) delim)
(mv (cons (first xs) before) after))))
 
(defun split (xs delim)
(if (endp xs)
nil
(mv-let (before after)
(split-at xs delim)
(cons before (split after delim)))))
 
(defun css->strs (css)
(if (endp css)
nil
(cons (coerce (first css) 'string)
(css->strs (rest css)))))
 
(defun split-str (str delim)
(css->strs (split (coerce str 'list) delim)))
 
(defun print-with (strs delim)
(if (endp strs)
(cw "~%")
(progn$ (cw (first strs))
(cw (coerce (list delim) 'string))
(print-with (rest strs) delim))))</lang>
 
Output:
<pre>&gt; (print-with (split-str "Hello,How,Are,You,Today" #\,) #\.)
Hello.How.Are.You.Today.</pre>
 
=={{header|ActionScript}}==
Anonymous user