Question: Quation: how to change the map function to 2D array? In this program, we can not use map, string, class, pointer, vector... here is the
Quation: how to change the map function to 2D array? In this program, we can not use map, string, class, pointer, vector...
here is the code:
#include
using namespace std;
map
void setPermOrder(string key) { //here we are adding the key for permutation order for(int i=0; i < key.length(); i++) { keyMap[key[i]] = i;// here } }
// method for encription string encryptMsg(string msg,string key) { int row,col,j; string cipher = ""; /* here we calculate column of the matrix*/ col = key.length(); /* here we calculate Maximum row of the matrix*/ row = msg.length()/col; if (msg.length() % col) row += 1;
char matrix[row][col];
for (int i=0,k=0; i < row; i++) { for (int j=0; j
for (map return cipher; } // method for decryption string decryptMsg(string msg,string key) { /* here we calculate row and column for cipher Matrix */ int col = key.length(); int row = msg.length()/col; char cipherMat[row][col]; /* here we add character into matrix column wise */ for (int j=0,k=0; j /* here we update the order of key for decryption */ int index = 0; for( map int main(void) // Driver Program which is used to start the program execution from here { int x; string cipher; string msg; cout<<"enter the message for encryption or decryption"< cout << " Please choose following options: "; cout << "1 = For Encryption press 1. "; cout << "2 = For Decryption press 2 "; cin >> x; switch(x) { //first case for encrypting a string case 1: cipher = encryptMsg(msg,key); cout << " Encrypted message: " << cipher << endl; break; //second case for decrypting a string case 2: cout << "Decrypted Message: " << decryptMsg(msg,key) << endl; break; default: cout << "Invalid option "; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
