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 #include

using namespace std;

map keyMap; ///???????????????how to modify it?

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::iterator ii = keyMap.begin(); ii!=keyMap.end(); ++ii) { j=ii->second; // in this loop we are getting cipher text from matrix column wise using permuted key for (int i=0; i

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::iterator ii=keyMap.begin(); ii!=keyMap.end(); ++ii) ii->second = index++; /*here we are Arranging the matrix column wise based on permutation order by adding into new matrix*/ char decCipher[row][col]; map::iterator ii=keyMap.begin(); int k = 0; for (int l=0,j; key[l]!='\0'; k++) { j = keyMap[key[l++]]; for (int i=0; i

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"<>msg; //user can read message from keyboard string key; cout<<"enter the key for encryption or decryption"<>key; //user can read key from keyboard setPermOrder(key);

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!