Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Last active May 3, 2020 23:27
Show Gist options
  • Save misterpoloy/e28626b423e7516c1b8e329602b8bfbb to your computer and use it in GitHub Desktop.
Save misterpoloy/e28626b423e7516c1b8e329602b8bfbb to your computer and use it in GitHub Desktop.
Shift string Code Challenge
using namespace std;
string caesarCypherEncryptor(string str, int key) {
for (int i = 0; i < str.size(); i++) {
int charCode = (int)str[i];
int nextCode = charCode + (key % 26); // Protect from max alphabet
int modus = (nextCode % 'z') + 'a' - 1; // Protect from max Z, and min A
// Add min in this case, a.
int resultCode = nextCode > 'z' ? modus : nextCode;
str[i] = (char)resultCode;
}
return str;
}
@misterpoloy
Copy link
Author

CodeChallenge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment