RosettaCodeData/Task/Reverse-a-string/OCaml/reverse-a-string-2.ocaml

16 lines
310 B
Text
Raw Permalink Normal View History

2019-09-12 10:33:56 -07:00
let rev_bytes bs =
let last = Bytes.length bs - 1 in
2013-04-10 23:57:08 -07:00
for i = 0 to last / 2 do
let j = last - i in
2019-09-12 10:33:56 -07:00
let c = Bytes.get bs i in
Bytes.set bs i (Bytes.get bs j);
Bytes.set bs j c;
2013-04-10 23:57:08 -07:00
done
2019-09-12 10:33:56 -07:00
let () =
let s = Bytes.of_string "Hello World" in
rev_bytes s;
print_bytes s;
print_newline ();
;;