RosettaCodeData/Task/Caesar-cipher/Ballerina/caesar-cipher.ballerina
2025-06-11 20:16:52 -04:00

32 lines
830 B
Text

import ballerina/io;
function encrypt(string s, int key) returns string {
final int offset = key % 26;
if offset == 0 { return s; }
int d;
int[] chars = [];
foreach int c in s.toCodePointInts() {
if c >= 64 && c <= 90 {
d = c + offset;
if d > 90 { d -= 26; }
} else if c >= 97 && c <= 122 {
d = c + offset;
if d > 122 { d -= 26; }
} else {
d = c;
}
chars.push(d);
}
return checkpanic string:fromCodePointInts(chars);
}
function decrypt(string s, int key) returns string {
return encrypt(s, 26 - key);
}
public function main() {
string encoded = encrypt("Bright vixens jump; dozy fowl quack.", 8);
io:println(encoded);
string decoded = decrypt(encoded, 8);
io:println(decoded);
}