xxxxxxxxxx
import std.stdio : writeln;
/**
Shifts every character in the
array `input` for `shift` characters.
The character range is limited to `a-z`
and the next character after z is a.
Params:
input = array to shift
shift = shift length for each char
Returns:
Shifted char array
*/
char[] encrypt(char[] input, char shift)
{
auto result = input.dup;
for (int i=0; i< result.length; i++) {
char ch = result[i];
int alphaIndex = ch - 'a'; // a -> 0, b -> 1, c -> 2, etc
int shiftedIndex = (alphaIndex + shift) % 26; // 26 chars in alphabet
int newValue = 'a' + shiftedIndex; // 0 -> a, 1 -> b, etc
result[i] = cast(char)newValue;
}
return result;
}
void main()
{
// We will now encrypt the message with
// Caesar encryption and a
// shift factor of 16!
char[] toBeEncrypted = [ 'w','e','l','c',
'o','m','e','t','o','d',
// The last , is okay and will just
// be ignored!
];
writeln("Before: ", toBeEncrypted);
auto encrypted = encrypt(toBeEncrypted, 16);
writeln("After: ", encrypted);
// Make sure we the algorithm works
// as expected
assert(encrypted == [ 'm','u','b','s','e',
'c','u','j','e','t' ]);
}