-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.ml
36 lines (30 loc) · 1.05 KB
/
encode.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
let enc (c:char) (off:int) : char =
let valc = Char.code (c) in
let sum = valc + off in
if valc >= 65 && valc <= 90 then
if (sum < 65) then Char.chr(91 - ((65 - sum) mod 26))
else if sum > 90 then Char.chr(((sum - 90) mod 26) + 64)
else Char.chr(sum)
else if valc >= 97 && valc <= 122 then
if (sum < 97) then Char.chr(123 - ((97 - sum) mod 26))
else if sum > 122 then Char.chr(((sum - 122) mod 26) + 96)
else Char.chr(sum)
else c;;
let encstr (s: string) (off: int) : string = String.map (fun x -> enc x off) s;;
let read_file filename = let ic = open_in filename in
let rec read_lines lines =
try
let line = input_line ic in
read_lines (line :: lines)
with End_of_file ->
close_in ic;
List.rev lines
in
read_lines []
;;
let encfile s off =let out = open_out (s^".enc") in
let f = read_file s in
List.iter (fun x -> Printf.fprintf out "%s\n" (encstr x off)) f;
close_out out
;;
encfile "one" 3;;