To find in a string, integer encryption using the Polybius Square Cipher.

  C & C ++ Interview Q&A

Polybius square cipher

It is a table that is used to convert letters into numbers. The table for English encryption is a 5X5 table, with 25 cells for 26 letters of an English dictionary. The letters i and j are placed together in a cell.

The following table shows a polybius square cipher –

1 2 3 4 5
1 A B C D E
2 F G H I, J K
3 L M N O P
4 Q R S T U
5 V W X Y Z

Tables can be randomized. In addition, the table size can be changed based on the letter of the language.

Let’s take an example to understand the problem

Input − Hello

Output − 2315313134

Example

#include <cmath>
#include <iostream>
using namespace std;
void LetterToNumber(string str) {
   int R, C;
   for (int i = 0; str[i]; i++) {
      R = ceil((str[i] - 'a') / 5) + 1;
      C = ((str[i] - 'a') % 5) + 1;
      if (str[i] == 'k') {
         R = R - 1;
         C = 5 - C + 1;
      }
      else if (str[i] >= 'j') {
         if (C == 1) {
            C = 6;
            R = R - 1;
         }
         C = C - 1;
      }
      cout<<R<<C;
   }
   cout << endl;
}
int main() {
   string str = "tutorialspoint";
   cout<<"The numeric encryption of string '"<<str<<"' is : ";
   LetterToNumber(str);
   return 0;
}

Output

The numeric encryption of string 'aitechtonic' is : 1124441513234434332413

 

LEAVE A COMMENT