Question
C++ need help finishing Polybius was an ancient Greek writer who first proposed a method of substituting different two-digit numbers for each letter. The alphabet
C++ need help finishing
Polybius was an ancient Greek writer who first proposed a method of substituting different two-digit numbers for each letter. The alphabet is written inside a 5-by-5 square matrix which has numbered rows and columns:
| 1 | 2 | 3 | 4 | 5 |
1 | A | B | C | D | E |
2 | F | G | H | I | J |
3 | K | L | M | N | O |
4 | P | Q | R | S | T |
5 | U | V | W | X | Y/Z |
Note that Y andZ are written in the last cell to divde the letters evenly. The context o the message should make clear which of the two letters is intended.
To encode, substitute for each letter the numbers marking the row and column in which the letter appears. Always put the row number first. The word WATERMELON is encoded in a string as:
53114515433315323534
To decode, simply locate each letter indicated by the number. The first number, 53, tells you to find the letter at the intersection of the fifth row and the third column.
Riddle:
If you stand so that you face east and your back is to the west, what is on your left hand?
Answer:
21243422154344
Write functions that use the Polybius cypher to encode and decode strings. In particular, decode the answer to the riddle.
#include
#include
using namespace std;
const int gsize = 5;
string encode(const string& plain);
string decode(const string& cipher);
bool search2d(char a2d[][gsize], char target, int &row, int &col);
int main()
{
string cipher1 = encode("SILENCE");
string cipher2 = encode("DAYS");
string cipher3 = encode("DAZE");
cout << cipher1 << endl;
cout << cipher2 << endl;
cout << cipher3 << endl;
decode(cipher1);
decode(cipher2);
decode(cipher3);
system("pause");
return 0;
}
string encode(const string& plain)
{
static char grid[][gsize]{
{'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'}
};
string cipher = "";
int row, col;
for (int pos = 0; pos < plain.length(); pos++)
{
if (search2d(grid, plain[pos], row, col))
cipher = cipher + to_string(row + 1) + to_string(col + 1);
else if (plain[pos] == 'Z')
cipher = cipher + to_string(5) + to_string(5);
else
cipher = cipher + " ";
}
return cipher;
}
string decode(const string& cipher)
{
static char grid[][gsize]{
{'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'}
};
for (int pos = 0; pos < cipher.length(); pos += 2)
{
int row = stoi(string(1, cipher[pos]));
int col = stoi(string(1, cipher[pos + 1]));
cout << row << col << ' ';
}
cout << endl;
return "";
}
bool search2d(char a2d[][gsize],
char target, int &row, int &col)
{
for (row = 0; row < gsize; row++)
for (col = 0; col < gsize; col++)
if (a2d[row][col] == target)
return true;
return false;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started