🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # acolburn's solution

## to Rotational Cipher in the Delphi Pascal Track

Published at Jan 18 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.

### Loading Exercises into Delphi

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

const
// lower case letters are ASCII 97-122
// upper case letters are ASCII 65-90
START_LOWER_CASE = 97;
END_LOWER_CASE = 122;
START_UPPER_CASE = 65;
END_UPPER_CASE = 90;

type
RotationalCipher = class
public
class function rotate(aString: string; key: integer): string;
private
class var fKey: integer;
class function rotateCase(aChar: char; caseStart: integer;
caseEnd: integer): char;
end;

implementation

uses SysUtils;

{ RotationalCipher }

class function RotationalCipher.rotate(aString: string; key: integer): string;
var
I: integer;
begin
fKey := key;
result := string.empty;
for I := 1 to length(aString) do
begin
if CharInSet(aString[I], ['a' .. 'z']) then
result := result + rotateCase(aString[I], START_LOWER_CASE,
END_LOWER_CASE)
else if CharInSet(aString[I], ['A' .. 'Z']) then
result := result + rotateCase(aString[I], START_UPPER_CASE,
END_UPPER_CASE)
else
result := result + aString[I];
end;
end;

class function RotationalCipher.rotateCase(aChar: char; caseStart: integer;
caseEnd: integer): char;
var
temp: integer;
begin
temp := Ord(aChar) + fKey;
if temp > caseEnd then
temp := temp - 26;
if temp < caseStart then
temp := temp + 26;

result := chr(temp);
end;

end.``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?