From 59a78b7b905258b1dba5bc0519ef6cad1367cda1 Mon Sep 17 00:00:00 2001 From: Ethan Davis Date: Sun, 29 Sep 2024 12:21:31 -0400 Subject: [PATCH 1/2] Added caesar cipher in c++ --- Caesars-Cipher/caesar-cipher.cpp | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Caesars-Cipher/caesar-cipher.cpp diff --git a/Caesars-Cipher/caesar-cipher.cpp b/Caesars-Cipher/caesar-cipher.cpp new file mode 100644 index 00000000..1253e2dc --- /dev/null +++ b/Caesars-Cipher/caesar-cipher.cpp @@ -0,0 +1,47 @@ +#include +#include + +using namespace std; + +void caesarShift(string& phrase, int shiftAmount); + +int main() { + + string phrase; + int shiftAmount; + + // Get the shift parameters + + cout << "Enter your phrase: "; + getline(cin, phrase); + + cout << "Enter your shift amount: "; + cin >> shiftAmount; + + caesarShift(phrase, shiftAmount); + + cout << endl << phrase; + + return 0; +} + +void caesarShift(string& phrase, int shiftAmount) { + // Caesar Shift a certain phrase by a certain amount + + int normalizedAmount = shiftAmount % 26; // Ensure that the shiftAmount keeps letters between a-z + + for (int i = 0; i < phrase.length(); i++) { // Shift each letter + + if (phrase[i] >= 65 && phrase[i] <= 90 && phrase[i] + normalizedAmount > 90) // Uppercase overflow + phrase[i] = phrase[i] + normalizedAmount - 26; + + else if (phrase[i] >= 97 && phrase[i] <= 122 && phrase[i] + normalizedAmount > 122) // Lowercase overflow + phrase[i] = phrase[i] + normalizedAmount - 26; + + else if (!(phrase[i] >= 65 && phrase[i] <= 90) && !(phrase[i] >= 97 && phrase[i] <= 122)) // Leave all not letter characters as they are + continue; + + else + phrase[i] = phrase[i] + normalizedAmount; + } +} \ No newline at end of file From b47db8482406d148d2fabc1c86ef25c7de220567 Mon Sep 17 00:00:00 2001 From: Ethan Davis Date: Sun, 29 Sep 2024 13:17:46 -0400 Subject: [PATCH 2/2] fixed a comment typo --- Caesars-Cipher/caesar-cipher.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Caesars-Cipher/caesar-cipher.cpp b/Caesars-Cipher/caesar-cipher.cpp index 1253e2dc..26386f9d 100644 --- a/Caesars-Cipher/caesar-cipher.cpp +++ b/Caesars-Cipher/caesar-cipher.cpp @@ -38,7 +38,7 @@ void caesarShift(string& phrase, int shiftAmount) { else if (phrase[i] >= 97 && phrase[i] <= 122 && phrase[i] + normalizedAmount > 122) // Lowercase overflow phrase[i] = phrase[i] + normalizedAmount - 26; - else if (!(phrase[i] >= 65 && phrase[i] <= 90) && !(phrase[i] >= 97 && phrase[i] <= 122)) // Leave all not letter characters as they are + else if (!(phrase[i] >= 65 && phrase[i] <= 90) && !(phrase[i] >= 97 && phrase[i] <= 122)) // Leave all non-letter characters as they are continue; else