/* * This is a header file that includes a translator class whose main use of this funtion will be to * translate strings of letters into strings of numbers that can be encoded, and transferign these * encoded strings of numbers back to strings of letters. The strings used here are from the C++ * string class and not C style strings since the former are easier to use. Also, note that the * letters and numbers are in correspondence according to the letters place in the alphabet with * startin at 0. Thus a <-> 0, b <-> 1, ..., z <-> 25, which is similar to the correspondence from * your cryptology reading except that the indexing starts at 0 to correspond to C++ array * indexing. Note that, as in the cryptology readings, only lower case letters are used, and the * strings must contain no spaces or punctuation to be translated properly. * */ //Note the #include declaration, which is necessary to use the string class. #include #include using namespace std; //Declares the Translator class. class Translator { private: //Variable that will hold the string that we use for translation. string tran; public: Translator() {tran = "abcdefghijklmnopqrstuvwxyz";} void string_to_num(string *str, int *arr, int n); void num_to_string(string *str, int *arr, int n); }; void Translator::string_to_num(string *str, int *arr, int n) { for(int i = 0; i < n; i++) { for(int j = 0; j < 26; j++) { if(tran[j] == (*str)[i]) { arr[i] = j; break; } } } } void Translator::num_to_string(string *str, int *arr, int n) { for(int i = 0; i < n; i++) { (*str)[i] = tran[*arr]; arr++; } }