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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
/*
Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher
(check Resources tab for more info) shifts each letter by a number of letters. If the shift takes you
past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by
3, w, x, y and z would map to z, a, b and c.
Create a function that takes a string s (text to be encrypted) and an integer k (the rotation factor).
It should return an encrypted string.
caesarCipher("Always-Look-on-the-Bright-Side-of-Life", 5)
➞ "Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj"
caesarCipher("A friend in need is a friend indeed", 20)
➞ "U zlcyhx ch hyyx cm u zlcyhx chxyyx"
*/
#include <iostream>
//#include "caesarsCipher"
std::string caesarsCipher(std::string message, int shift){
std::string encrypted_message;
int loop_shift = shift;
for(size_t i=0;i<=message.length();i++){
loop_shift = shift;
// Take each character c, sort it into ASCII capitals, ASCII lower case, or space / dash
char c = message[i];
//std::cout << c << std::endl;
// If c is a capital letter
if(c >= 65 && c <=90){
//std::cout << "c is a capital " << c << std::endl;
// If there is a wrap-around
//std::cout << "before the do if loop shift was " << shift << " and c was " << c << std::endl;
do{
if(c + loop_shift > 90){
//std::cout << "Inside the uppercase while loop because the sum is ";
//std::cout << c + shift << " which is greater than 90" << std::endl;
// Reduce the shift by the remaining amount between c and the end of the alphabet
loop_shift = loop_shift - (90 - c) - 1;
//std::cout << "Now the shift is " << shift << std::endl;
// Reset c to base value, A
c = char(65);
}
}
while(c + loop_shift > 90);
//std::cout << "after the do if loop shift is " << shift << " and c is " << c << std::endl;
encrypted_message += char(c + loop_shift);
}
// If c is a lowercase letter
else if(c >= 97 && c <=122){
//std::cout << "c is a lowercase " << c << std::endl;
// If there is a wrap-around
//std::cout << "before the do if loop shift was " << shift << " and c was " << c << std::endl;
do{
if(c + loop_shift > 122){
//std::cout << "Inside the lowercase while loop because the sum is ";
//std::cout << c + shift << " which is greater than 122" << std::endl;
// Reduce the shift by the remaining amount between c and the end of the alphabet
loop_shift = loop_shift - (122 - c) - 1;
//std::cout << "Now the shift is " << shift << std::endl;
// Reset c to base value, A
c = char(97);
}
}
while(c + loop_shift > 122);
//std::cout << "after the do if loop shift is " << shift << " and c is " << c << std::endl;
encrypted_message += char(c + loop_shift);
}
else{
encrypted_message += c;
}
//std::cout << "c is " << int(c) << "\tshift is " << shift;
//std::cout << "\tTheir sum is " << c + shift << std::endl;
//std::cout << "The character that creates is " << char(c+shift) << std::endl;
//std::cout << "The encrypted sequence is " << encrypted_message[i] << std::endl;
}
std::cout << encrypted_message[1];
return encrypted_message;
}
int main(){
std::string message_in;
int shift_in;
shift_in = 5;
message_in = "Always-Look-on-the-Bright-Side-of-Life";
//message_in = "Always-L";
std::string encryption = caesarsCipher(message_in, shift_in);
std::cout << encryption << std::endl;
}
|