In: Computer Science
Q: IN C++ - Redesign the CaesarCipher class as a subclass of the SubstitutionCipher from the previous problem
my code is:
#include <iostream>
#include <string>
using namespace std;
class SubstitutionCipher {
public:
//Define your key which is a permutation of 26 alphabets
string key;
//Constructor to create an instance which initializes the key
with the given permutation
SubstitutionCipher(string k) {
key = k;
}
//function to encode a string
string encode(string data) {
//initialize encoded data
string encodedData = data;
//replace each character in encodedData with the corresponding
mapped data in key. We subtract a char with 'A' to get its
position
for (int i = 0; i < data.length(); i++) {
encodedData[i] = key.at(data.at(i) - 'A');
}
//return the encoded data
return encodedData;
}
//function to decode a string
string decode(string data) {
//initialize decoded data
string decodedData = data;
//replace each character in decodedData with the corresponding
reverse mapped data in key. We add the position with 'A' to get the
corresponding char
for (int i = 0; i < data.length(); i++) {
decodedData[i] = key.find(data.at(i)) + 'A';
}
//return the decoded data
return decodedData;
}
};
//main() method to test the implementation
int main() {
SubstitutionCipher cipher("ZYXWVUTSRQPONMLKJIHGFEDCBA");
string data = "DEBAJYOTI";
string encodedData = cipher.encode(data);
cout << data << " encoded as " << encodedData
<< endl;
string decodedData = cipher.decode(encodedData);
cout << encodedData << " decoded as " <<
decodedData;
}
#include <iostream>
#include <string>
using namespace std;
class SubstitutionCipher {
public:
//Define your key which is a permutation of 26 alphabets
string key;
//Constructor to create an instance which initializes the key
with the given permutation
SubstitutionCipher(string k) {
key = k;
}
//function to encode a string
string encode(string data) {
//initialize encoded data
string encodedData = data;
//replace each character in encodedData with the corresponding
mapped data in key. We subtract a char with 'A' to get its
position
for (int i = 0; i < data.length(); i++) {
encodedData[i] = key.at(data.at(i) - 'A');
}
//return the encoded data
return encodedData;
}
//function to decode a string
string decode(string data) {
//initialize decoded data
string decodedData = data;
//replace each character in decodedData with the corresponding
reverse mapped data in key. We add the position with 'A' to get the
corresponding char
for (int i = 0; i < data.length(); i++) {
decodedData[i] = key.find(data.at(i)) + 'A';
}
//return the decoded data
return decodedData;
}
};
class ccipher: public SubstitutionCipher
{
public:
void encrypt(string text, int s)
{
string result = "";
// traverse text
for (int i=0;i<text.length();i++)
{
// apply transformation to each
character
// Encrypt Uppercase letters
if (isupper(text[i]))
result +=
char(int(text[i]+s-65)%26 +65);
// Encrypt Lowercase letters
else
result += char(int(text[i]+s-97)%26
+97);
}
// Return the resulting string
// return result;
cout << "\n C_Cipher: " <<result;
}
};
//main() method to test the implementation
int main() {
SubstitutionCipher cipher("ZYXWVUTSRQPONMLKJIHGFEDCBA");
string data = "DEBAJYOTI";
string encodedData = cipher.encode(data);
cout << data << " encoded as " << encodedData
<< endl;
string decodedData = cipher.decode(encodedData);
cout << encodedData << " decoded as " <<
decodedData;
ccipher cc;
cc.encrypt(data, 4);
}