简体   繁体   中英

I can't figure out what's wrong with my code

I'm trying to make a program that takes a coded message and decodes Rot 13 and Rot 6 ciphers. The Rot 13 part works just fine, but the Rot 6 part only works in specific instances ( input "Yngqkt, tuz yzoxxkj" should translate to "Shaken, not stirred" but instead, it's returning as "Yhmkqn not stirrqp")

const int lCaseA = 97; 
const int lCaseM = 109;
const int lCaseN = 110;
const int lCaseZ = 122;
const int uCaseA = 65;
const int uCaseM = 77;
const int uCaseN = 78;
const int uCaseY = 89;
const int uCaseZ = 90;


string rot6(string input) {
 int inputSize = input.size();
 int index{};

 while (index != inputSize) {
  if (input[index] >= lCaseA && input[index] <= lCaseM)    
   input[index] = input[index] + 6;    
  else if (input[index] >= lCaseN && input[index] <= lCaseZ)    
   input[index] = input[index] - 6;    
  else if (input[index] >= uCaseA && input[index] <= uCaseM)    
   input[index] = input[index] + 6;
  else if (input[index] <= uCaseN && input[index] <= uCaseZ)    
   input[index] = input[index] - 6;    
  index++;
 }    
 return input;
}

string rot13(string input) {  //Decodes into rot 13    
 int inputSize = input.size();    
 int index{};    
 while (index != inputSize) {    
  if (input[index] >= lCaseA && input[index] <= lCaseM)    
   input[index] = input[index] + 13;    
  else if (input[index] >= lCaseN && input[index] <= lCaseZ)    
   input[index] = input[index] - 13;    
  else if (input[index] >= uCaseA && input[index] <= uCaseM)    
   input[index] = input[index] + 13;    
  else if (input[index] <= uCaseN && input[index] <= uCaseZ)    
   input[index] = input[index] - 13;           
  index++;
 }        
 return input;
}


int main() {    
 string plaintext;    
 string ans13;    
 string ans6;    
 string ansCoffee;    

 cout << "Whats the message Spy Guy: ";    
 getline(cin, plaintext);    
 ans13 = rot13(plaintext);    
 ans6 = rot6(plaintext);

 cout << "One of these is your decoded message" << endl << "In Rot 13:  " << ans13 << endl << "In Rot 6:  " << ans6 << endl;
 return 0;
}

Only ROT13 is reversible because it moves by half of alphabet size.

If you ROT6 "Shaken, not stirred" you get "Yngqkt, tuz yzoxxkj" but when you ROT6 that again, you won't get "Shaken, not stirred" back. Check https://rot13.com/ .

And your implementation of ROT6 is also wrong. You just used ROT13 implementation and changed 13 to 6 . But ROT13 implementation relies on the fact that 13 is half of alphabet size. That is not true for ROT6. If you want to use the same pattern in ROT6 implementation you have to divide the alphabet not in half, but in at and uz ranges. And then add 6 if the input letter falls into the first range and subtract 20 if the input letter falls into the second range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM