diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2022-11-18 18:07:04 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2022-11-18 18:07:04 -0800 |
| commit | 152e763148c6111ab2f42282149fdf42083d3177 (patch) | |
| tree | 61da413c9d97e858983533acd24f54d3c408c4ac | |
init
| -rw-r--r-- | caeser/caeser.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/caeser/caeser.cpp b/caeser/caeser.cpp new file mode 100644 index 0000000..e2bd114 --- /dev/null +++ b/caeser/caeser.cpp @@ -0,0 +1,47 @@ +#include <iostream> +#include <cstring> +int char_to_int(char *c) { + int value = 0; + int sign = 1; + if(*c == '-') { + sign = -1; + c++; + } + while(*c != '\0') { + value *= 10; + value += *c - '0'; + c++; + } + return value * sign; +} +int main(int argc, char *argv[]) { + if(argc != 3) { + throw std::invalid_argument("Wrong number of parameters! Run like this: \"caeser x texthere\" where X is the desired shift amount"); + } + int inputLength = strlen(argv[2]); + int shift = char_to_int(argv[1]); + while (shift < 0) { + shift += 26; + } + char *output = (char*) malloc(inputLength); + for(int i = 0; i < inputLength; i++) { + int cVal = argv[2][i]; + int rStart; + if(cVal >= 'A' && cVal <= 'Z') { + rStart = 'A'; + } + else if(cVal >= 'a' && cVal <= 'z'){ + rStart = 'a'; + } + else { + output[i] = cVal; + continue; + } + int rVal = cVal - rStart; + rVal = (rVal + shift) % 26; + output[i] = rVal + rStart; + } + std::cout << output << '\n'; + free(output); + return 0; +} |
