exercism-c/rail-fence-cipher
Blizzard Finnegan b92478b09c
Init commit
Leap, Difference of Squares, Grains, Collatz Conjecture, Queen Attack,
Darts, Hamming, and Space Age completed yesterday.

Binary and Linked List completed today.
2025-01-11 18:45:47 -05:00
..
.exercism Init commit 2025-01-11 18:45:47 -05:00
test-framework Init commit 2025-01-11 18:45:47 -05:00
HELP.md Init commit 2025-01-11 18:45:47 -05:00
makefile Init commit 2025-01-11 18:45:47 -05:00
rail_fence_cipher.c Init commit 2025-01-11 18:45:47 -05:00
rail_fence_cipher.h Init commit 2025-01-11 18:45:47 -05:00
README.md Init commit 2025-01-11 18:45:47 -05:00
test_rail_fence_cipher.c Init commit 2025-01-11 18:45:47 -05:00

Rail Fence Cipher

Welcome to Rail Fence Cipher on Exercism's C Track. If you need help running the tests or submitting your code, check out HELP.md.

Instructions

Implement encoding and decoding for the rail fence cipher.

The Rail Fence cipher is a form of transposition cipher that gets its name from the way in which it's encoded. It was already used by the ancient Greeks.

In the Rail Fence cipher, the message is written downwards on successive "rails" of an imaginary fence, then moving up when we get to the bottom (like a zig-zag). Finally the message is then read off in rows.

For example, using three "rails" and the message "WE ARE DISCOVERED FLEE AT ONCE", the cipherer writes out:

W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .

Then reads off:

WECRLTEERDSOEEFEAOCAIVDEN

To decrypt a message you take the zig-zag shape and fill the ciphertext along the rows.

? . . . ? . . . ? . . . ? . . . ? . . . ? . . . ?
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .

The first row has seven spots that can be filled with "WECRLTE".

W . . . E . . . C . . . R . . . L . . . T . . . E
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .

Now the 2nd row takes "ERDSOEEFEAOC".

W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .

Leaving "AIVDEN" for the last row.

W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .

If you now read along the zig-zag shape you can read the original message.

Source

Created by

  • @patricksjackson

Contributed to by

  • @ryanplusplus
  • @wolf99

Based on

Wikipedia - https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher