Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
33
Task/Caesar-cipher/Vala/caesar-cipher.vala
Normal file
33
Task/Caesar-cipher/Vala/caesar-cipher.vala
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
static void println(string str) {
|
||||
stdout.printf("%s\r\n", str);
|
||||
}
|
||||
|
||||
static unichar encrypt_char(unichar ch, int code) {
|
||||
if (!ch.isalpha()) return ch;
|
||||
|
||||
unichar offset = ch.isupper() ? 'A' : 'a';
|
||||
return (unichar)((ch + code - offset) % 26 + offset);
|
||||
}
|
||||
|
||||
static string encrypt(string input, int code) {
|
||||
var builder = new StringBuilder();
|
||||
|
||||
unichar c;
|
||||
for (int i = 0; input.get_next_char(ref i, out c);) {
|
||||
builder.append_unichar(encrypt_char(c, code));
|
||||
}
|
||||
|
||||
return builder.str;
|
||||
}
|
||||
|
||||
static string decrypt(string input, int code) {
|
||||
return encrypt(input, 26 - code);
|
||||
}
|
||||
|
||||
const string test_case = "The quick brown fox jumped over the lazy dog";
|
||||
|
||||
void main() {
|
||||
println(test_case);
|
||||
println(encrypt(test_case, -1));
|
||||
println(decrypt(encrypt(test_case, -1), -1));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue