I have written this code for practice. Here we will enter the number by which we want to shift alphabets. For eg.
Enter by how many numbers you want to shift alphabets: 4
Enter string: Programmer C++
Encrypted String: Lnkcnwiian Y++
Decryted String: Programmer C++
Review this code and suggest better C++11 and C++14 alternatives.
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
class Cipher
{
unsigned n;
const std::vector<char> alphabetL { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z' };
const std::vector<char> alphabetS { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z' };
std::unordered_map<char, char> shiftByNL;
std::unordered_map<char, char> shiftByNS;
public:
Cipher(unsigned n = 0);
std::string encrypt(std::string&);
std::string decrypt(std::string&);
private:
char byValue(const char); //Find by value
};
Cipher::Cipher(unsigned n)
{
for (int i = 0; i < n; i++)
{
shiftByNL.insert(std::make_pair(alphabetL[i], alphabetL[(26 - n) + i]));
shiftByNS.insert(std::make_pair(alphabetS[i], alphabetS[(26 - n) + i]));
}
for (int i = n; i < 26; i++)
{
shiftByNL.insert(std::make_pair(alphabetL[i], alphabetL[i - n]));
shiftByNS.insert(std::make_pair(alphabetS[i], alphabetS[i - n]));
}
}
char Cipher::byValue(const char value)
{
for (auto itL = shiftByNL.begin(), itS = shiftByNS.begin();
itL != shiftByNL.end(), itS != shiftByNS.end(); ++itL, ++itS)
{
if (itL->second == value)
{
return itL->first;
}
else if (itS->second == value)
{
return itS->first;
}
}
//not an alphabet
return value;
}
std::string Cipher::encrypt(std::string& original)
{
std::string encrypted;
std::size_t len = original.length();
for (std::size_t i = 0; i < len; i++)
{
if (original[i] == ' ')
{
encrypted = encrypted + ' ';
}
else if (shiftByNS.find(original[i]) != shiftByNS.end())
{
auto search = shiftByNS.find(original[i]);
encrypted = encrypted + search->second;
}
else if (shiftByNL.find(original[i]) != shiftByNL.end())
{
auto search = shiftByNL.find(original[i]);
encrypted = encrypted + search->second;
}
else //not an alphabet
{
encrypted = encrypted + original[i];
}
}
return encrypted;
}
std::string Cipher::decrypt(std::string& encrypted)
{
std::string decrypted;
std::size_t len = encrypted.length();
for (std::size_t i = 0; i < len; i++)
{
if (encrypted[i] == ' ')
{
decrypted = decrypted + ' ';
}
else
{
char key = byValue(encrypted[i]);
decrypted = decrypted + key;
}
}
return decrypted;
}
int main()
{
unsigned num;
std::cout << "Enter by how many numbers you want to shift alphabets: ";
std::cin >> num;
std::cin.ignore();
Cipher cipher(num);
std::string str;
std::cout << "Enter string: ";
std::getline(std::cin, str);
std::string res = cipher.encrypt(str);
std::cout << "Encrypted String: ";
std::cout << res << '\n';
std::cout << "Decryted String: ";
std::string decrypted = cipher.decrypt(res);
std::cout << decrypted << '\n';
}