diff --git a/Caesars-Cipher/caesar-cipher.cpp b/Caesars-Cipher/caesar-cipher.cpp new file mode 100644 index 00000000..26386f9d --- /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 non-letter characters as they are + continue; + + else + phrase[i] = phrase[i] + normalizedAmount; + } +} \ No newline at end of file