RosettaCodeData/Task/Strip-comments-from-a-string/OCaml/strip-comments-from-a-string-1.ml
2024-10-16 18:07:41 -07:00

20 lines
450 B
OCaml

let strip_comments str =
let len = String.length str in
let rec aux print i =
if i >= len then () else
match str.[i] with
| '#' | ';' ->
aux false (succ i)
| '\n' ->
print_char '\n';
aux true (succ i)
| c ->
if print then print_char c;
aux print (succ i)
in
aux true 0
let () =
strip_comments "apples, pears # and bananas\n";
strip_comments "apples, pears ; and bananas\n";
;;