# gottaTw's solution

## to Rotational Cipher in the Delphi Pascal Track

Published at Mar 16 2021 · 0 comments
Instructions
Test suite
Solution

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`. Using a key of `0` or `26` will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.

The general notation for rotational ciphers is `ROT + <key>`. The most commonly used rotational cipher is `ROT13`.

A `ROT13` on the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
``````

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

## Examples

• ROT5 `omg` gives `trl`
• ROT0 `c` gives `c`
• ROT26 `Cool` gives `Cool`
• ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
• ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`

## Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

If Delphi is properly installed, and `*.dpr` file types have been associated with Delphi, then double clicking the supplied `*.dpr` file will start Delphi and load the exercise/project. `control + F9` is the keyboard shortcut to compile the project or pressing `F9` will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the `File` drop down menu.

### When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

### Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the `exercism/delphi/<exerciseName>` directory.

For example, if you're submitting `ubob.pas` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas`.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you may request help from a mentor.

### uRotationalCipherTests.pas

``````unit uRotationalCipherTests;

interface
uses
DUnitX.TestFramework;

const
CanonicalVersion = '1.2.0.1';

type

[TestFixture('Test rotation from English to ROTn')]
RotationalCipherTests = class(TObject)
public
[Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
procedure rotate_a_by_0_same_output_as_input;

[Test]
[Ignore]
procedure rotate_a_by_1;

[Test]
[Ignore]
procedure rotate_a_by_26_same_output_as_input;

[Test]
[Ignore]
procedure rotate_m_by_13;

[Test]
[Ignore]
procedure rotate_n_by_13_with_wrap_around_alphabet;

[Test]
[Ignore]
procedure rotate_capital_letters;

[Test]
[Ignore]
procedure rotate_spaces;

[Test]
[Ignore]
procedure rotate_numbers;

[Test]
[Ignore]
procedure rotate_punctuation;

[Test]
[Ignore]
procedure rotate_all_letters;

[Test]
[Ignore('Optional: Challenge Test')]
procedure rotate_m_by_negative_1;

[Test]
[Ignore('Optional: Challenge Test')]
procedure rotate_A_by_negative_2;

[Test]
[Ignore('Optional: Challenge Test')]
procedure rotate_letters_by_negative_26;
end;

implementation
uses sysutils, uRotationalCipher;

procedure RotationalCipherTests.rotate_a_by_0_same_output_as_input;
begin
Assert.AreEqual('a', RotationalCipher.rotate('a', 0));
end;

procedure RotationalCipherTests.rotate_a_by_1;
begin
Assert.AreEqual('b', RotationalCipher.rotate('a', 1));
end;

procedure RotationalCipherTests.rotate_a_by_26_same_output_as_input;
begin
Assert.AreEqual('a', RotationalCipher.rotate('a', 26));
end;

procedure RotationalCipherTests.rotate_m_by_13;
begin
Assert.AreEqual('z', RotationalCipher.rotate('m', 13));
end;

procedure RotationalCipherTests.rotate_n_by_13_with_wrap_around_alphabet;
begin
Assert.AreEqual('a', RotationalCipher.rotate('n', 13));
end;

procedure RotationalCipherTests.rotate_capital_letters;
begin
Assert.AreEqual('TRL', RotationalCipher.rotate('OMG', 5));
end;

procedure RotationalCipherTests.rotate_spaces;
begin
Assert.AreEqual('T R L', RotationalCipher.rotate('O M G', 5));
end;

procedure RotationalCipherTests.rotate_numbers;
begin
Assert.AreEqual('Xiwxmrk 1 2 3 xiwxmrk',
RotationalCipher.rotate('Testing 1 2 3 testing', 4));
end;

procedure RotationalCipherTests.rotate_punctuation;
begin
Assert.AreEqual('Gzo''n zvo, Bmviyhv!',
RotationalCipher.rotate('Let''s eat, Grandma!', 21));
end;

procedure RotationalCipherTests.rotate_all_letters;
begin
Assert.AreEqual('Gur dhvpx oebja sbk whzcf bire gur ynml qbt.',
RotationalCipher.rotate('The quick brown fox jumps over the lazy dog.', 13));
end;

{\$region 'Bonus Tests'}
procedure RotationalCipherTests.rotate_m_by_negative_1;
begin
Assert.AreEqual('l',
RotationalCipher.rotate('m', -1));
end;

procedure RotationalCipherTests.rotate_A_by_negative_2;
begin
Assert.AreEqual('Y',
RotationalCipher.rotate('A', -2));
end;

procedure RotationalCipherTests.rotate_letters_by_negative_26;
begin
Assert.AreEqual('OMG',
RotationalCipher.rotate('OMG', -26));
end;
{\$endregion 'Bonus Tests'}

initialization
TDUnitX.RegisterTestFixture(RotationalCipherTests);
end.``````
``````unit uRotationalCipher;

interface

type
RotationalCipher = class

public
class function rotate(const aStr: string; const aValue: Integer): string;
end;

implementation
uses
System.Character, System.SysUtils;

function ConvertToNumber(const C: Char; const aIsUpper: Boolean): Integer;
begin
Result := Ord(C);
if aIsUpper then
Result := Result - Ord('A')
else
Result := Result - Ord('a');
end;

function ConvertBackToChar(const aValue: Integer; const aIsUpper: Boolean): Char;
begin
if aIsUpper then
Result := Chr(aValue + Ord('A'))
else
Result := Chr(aValue + Ord('a'));
end;

function CalcRotation(const C: Char; const aValue: Integer): Char;
var
IsUpper: Boolean;
lCharAsInt: Integer;
begin
IsUpper := C.IsUpper;

lCharAsInt := ConvertToNumber(C, IsUpper);
Inc(lCharAsInt, aValue);

while lCharAsInt < 0 do
Inc(lCharAsInt, 26);
lCharAsInt := lCharAsInt mod 26;

Result := ConvertBackToChar(lCharAsInt, IsUpper);
end;

class function RotationalCipher.rotate(const aStr: string; const aValue: Integer): string;
var
Idx: Integer;
begin
Result := aStr;
for Idx := 0 to Length(aStr) do
begin
if not aStr[Idx].IsLetter then
Result[Idx] := aStr[Idx]
else
Result[Idx] := CalcRotation(aStr[Idx], aValue);
end;

end;

//initialization
//finalization

end.``````