fork download
  1. /*
  2.  
  3.  The purpose of this program is to emulate the workings of a simple substitution
  4.  cypher. The cypher does not distinguish between "encrypted" or "decrypted"
  5.  states; rather it simply translates between the characters contained in the
  6.  cypher and allows plain passthrough of the remaining characters.
  7.  
  8.  Here's an example of a valid, albeit weak, cypher:
  9.  
  10.   TENIS
  11.   -----
  12.   POLAR
  13.  
  14.  This cypher is valid because it contains two rows that are of the same length
  15.  and contain distinct characters - no two equal characters appear in any row.
  16.  
  17.  The cyphers may contain any character (alphanumeric, non-alphanumeric,
  18.  accented, extended Unicode...) depending on the complexity of the cypher you
  19.  wish to have provided that character collisions do not happen.
  20.  
  21.  */
  22.  
  23. #include <iostream>
  24. #include <string>
  25. #include <vector>
  26.  
  27. typedef std::vector<char> cypherrow;
  28.  
  29. bool validate_cypher(cypherrow row1, cypherrow row2)
  30. {
  31. bool statuscode = true;
  32. for (auto letter1 : row1) {
  33. for (auto letter2 : row2) {
  34. if (letter1 == letter2) {
  35. std::cout << "Warning: cypher collision detected at '" << letter1 << "'!\n";
  36. statuscode = false;
  37. }
  38. }
  39. }
  40. return statuscode;
  41. }
  42.  
  43. // Begin translation. Note that it's symmetrical, as in we don't exactly have
  44. // a "state" saying encypted or decrypted. The cypher takes a letter, sees if
  45. // it has it available and rotates it. That's it.
  46.  
  47. std::string translate(std::string input, cypherrow r1, cypherrow r2) {
  48. std::string output = ""; // this will become the final string.
  49. bool match = false;
  50. int row(0);
  51. int index(0);
  52. for (auto letter : input) {
  53. match = false;
  54. for (auto letter1 = r1.begin();
  55. letter1 != r1.end(); ++letter1) {
  56. if (letter == *letter1) {
  57. // we got a match!
  58. match = true;
  59. row = 1;
  60. index = letter1 - r1.begin();
  61. break;
  62. }
  63. }
  64. if (match == false) {
  65. for (auto letter2 = r2.begin();
  66. letter2 != r2.end(); ++letter2) {
  67. if (letter == *letter2) {
  68. // we got a match!
  69. match = true;
  70. row = 2;
  71. index = letter2 - r2.begin();
  72. break;
  73. }
  74. }
  75. }
  76. // uncomment the following block for debugging!
  77. /*
  78.   std::cout << "Now manipulating letter " << letter << ", match is "
  79.   << match << " row is " << row << " index is " << index << std::endl;
  80.   */
  81. if (match) {
  82. std::string newletter;
  83. // get the letter in the opposite row at the same index:
  84. if (row == 1) {
  85. newletter = r2.at(index);
  86. }
  87. else if (row == 2) {
  88. newletter = r1.at(index);
  89. }
  90. // compose the new string by appending it to the string:
  91. output.append(newletter);
  92. }
  93. else {
  94. // push the character unchanged:
  95. std::string unchanged;
  96. unchanged = std::string(1, (char)letter);
  97. output.append(unchanged);
  98. }
  99. }
  100. return output;
  101. }
  102.  
  103. int main(int argc, char * argv[])
  104. {
  105. cypherrow row1 {'p','l','e','a','i','z','o','m'};
  106. cypherrow row2 {'b','n','u','f','c','s','y','r'};
  107. if (validate_cypher(row1, row2)) {
  108. std::cout << "Cypher is good.\n";
  109. }
  110. else {
  111. std::cout << "You have a corrupted cypher.\n";
  112. std::cout << "Please fix it before running this program.\n";
  113. return 1;
  114. }
  115. std::string samplestring;
  116. std::cout << "Enter the text to be translated:\n>_ ";
  117. std::getline(std::cin, samplestring);
  118. std::cout << translate(samplestring, row1, row2) << std::endl;
  119. return 0;
  120. }
Success #stdin #stdout 0s 3464KB
stdin
THIS ENTIRE BLOCK OF TEXT WILL BE PARSED WITHOUT ANY MODIFICATIONS BECAUSE IT CONTAINS ONLY UPPERCASE TEXT.
stdout
Cypher is good.
Enter the text to be translated:
>_ THIS ENTIRE BLOCK OF TEXT WILL BE PARSED WITHOUT ANY MODIFICATIONS BECAUSE IT CONTAINS ONLY UPPERCASE TEXT.