-
Notifications
You must be signed in to change notification settings - Fork 2
/
caesar_cipher.cpp
42 lines (36 loc) · 1.19 KB
/
caesar_cipher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
Caesar Cipher
#encoding #hashmap #string manipulation
Have the function CaesarCipher(str,num) take the str parameter and perform a Caesar
Cipher shift on it using the num parameter as the shifting number. A Caesar Cipher
works by shifting each letter in the string N places in the alphabet (in this case
N will be num). Punctuation, spaces, and capitalization should remain intact. For
example if the string is "Caesar Cipher" and num is 2 the output should be "Ecguct
Ekrjgt".
Optimal: o(-), achieved: o(-) - not calculated
*/
#include <iostream>
#include <string>
using namespace std;
char shift_char(char s, int st, int en, int num) {
if (s + num > en) {return static_cast<char>(st+s+num-en-1);}
else {return static_cast<char>(num+s);}
}
string CaesarCipher(string str, int num) {
char s;
// code goes here
for (int i{0}; i<str.length(); i++) {
s = str.at(i);
if (s <= 122 && s >= 97) { //lowercase
str.at(i) = shift_char(s,97,122,num);
} else if (s <= 90 && s >= 65) {
str.at(i) = shift_char(s,65,90,num);
}
}
return str;
}
int main(void) {
// keep this function call here
cout << CaesarCipher(coderbyteInternalStdinFunction(stdin));
return 0;
}