Skip to content

Commit

Permalink
Add exercise rotational-cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
ahans committed Apr 15, 2024
1 parent 7055555 commit 764bd73
Show file tree
Hide file tree
Showing 13 changed files with 4,085 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,20 @@
"control_flow_loops",
"structs"
]
},
{
"slug": "rotational-cipher",
"name": "Rotational Cipher",
"uuid": "3adbc85c-7e7e-4122-a37d-c843196bc8af",
"practices": [],
"prerequisites": [],
"difficulty": 2,
"topics": [
"control_flow_if_statements",
"control_flow_loops",
"memory_management",
"strings"
]
}
]
},
Expand Down
29 changes: 29 additions & 0 deletions exercises/practice/rotational-cipher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Instructions

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:

```text
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.`
21 changes: 21 additions & 0 deletions exercises/practice/rotational-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"ahans"
],
"files": {
"solution": [
"rotational_cipher.c",
"rotational_cipher.h"
],
"test": [
"test_rotational_cipher.c"
],
"example": [
".meta/example.c",
".meta/example.h"
]
},
"blurb": "Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Caesar_cipher"
}
21 changes: 21 additions & 0 deletions exercises/practice/rotational-cipher/.meta/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "rotational_cipher.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

char *rotate(const char *text, int shift_key)
{
char *rotated = malloc(strlen(text) + 1);
char *out = rotated;
for (const char *in = text; *in != '\0'; ++in, ++out) {
if (isalpha(*in)) {
const char base = isupper(*in) ? 'A' : 'a';
*out = base + ((*in - base + shift_key) % 26);
} else {
*out = *in;
}
}
*out = '\0';
return rotated;
}
6 changes: 6 additions & 0 deletions exercises/practice/rotational-cipher/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef ROTATIONAL_CIPHER_H
#define ROTATIONAL_CIPHER_H

char *rotate(const char *text, int shift_key);

#endif
40 changes: 40 additions & 0 deletions exercises/practice/rotational-cipher/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[74e58a38-e484-43f1-9466-877a7515e10f]
description = "rotate a by 0, same output as input"

[7ee352c6-e6b0-4930-b903-d09943ecb8f5]
description = "rotate a by 1"

[edf0a733-4231-4594-a5ee-46a4009ad764]
description = "rotate a by 26, same output as input"

[e3e82cb9-2a5b-403f-9931-e43213879300]
description = "rotate m by 13"

[19f9eb78-e2ad-4da4-8fe3-9291d47c1709]
description = "rotate n by 13 with wrap around alphabet"

[a116aef4-225b-4da9-884f-e8023ca6408a]
description = "rotate capital letters"

[71b541bb-819c-4dc6-a9c3-132ef9bb737b]
description = "rotate spaces"

[ef32601d-e9ef-4b29-b2b5-8971392282e6]
description = "rotate numbers"

[32dd74f6-db2b-41a6-b02c-82eb4f93e549]
description = "rotate punctuation"

[9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9]
description = "rotate all letters"
37 changes: 37 additions & 0 deletions exercises/practice/rotational-cipher/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### If you wish to use extra libraries (math.h for instance),
### add their flags here (-lm in our case) in the "LIBS" variable.

LIBS = -lm

###
CFLAGS = -std=c99
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -pedantic
CFLAGS += -Werror
CFLAGS += -Wmissing-declarations
CFLAGS += -DUNITY_SUPPORT_64 -DUNITY_OUTPUT_COLOR

ASANFLAGS = -fsanitize=address
ASANFLAGS += -fno-common
ASANFLAGS += -fno-omit-frame-pointer

.PHONY: test
test: tests.out
@./tests.out

.PHONY: memcheck
memcheck: ./*.c ./*.h
@echo Compiling $@
@$(CC) $(ASANFLAGS) $(CFLAGS) test-framework/unity.c ./*.c -o memcheck.out $(LIBS)
@./memcheck.out
@echo "Memory check passed"

.PHONY: clean
clean:
rm -rf *.o *.out *.out.dSYM

tests.out: ./*.c ./*.h
@echo Compiling $@
@$(CC) $(CFLAGS) test-framework/unity.c ./*.c -o tests.out $(LIBS)
1 change: 1 addition & 0 deletions exercises/practice/rotational-cipher/rotational_cipher.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "rotational_cipher.h"
6 changes: 6 additions & 0 deletions exercises/practice/rotational-cipher/rotational_cipher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef ROTATIONAL_CIPHER_H
#define ROTATIONAL_CIPHER_H

char *rotate(const char *text, int shift_key);

#endif
Loading

0 comments on commit 764bd73

Please sign in to comment.