BLARG
BLARG

Reputation: 27

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;
}

Upvotes: 1

Views: 271

Answers (2)

oria kali
oria kali

Reputation: 16

The conditions within the statement else if (input[index] <= uCaseN && input[index] <= uCaseZ), of the rot13 function, are both evaluating if input[index] is less than or equal to uCaseN, causing it to always evaluate as true. This results in all characters meeting the condition being shifted by -13, which is incorrect.

To correct this, change the statement to:

else if (input[index] >= uCaseN && input[index] <= uCaseZ)

Upvotes: 0

danadam
danadam

Reputation: 3450

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 a-t and u-z 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.

Upvotes: 2

Related Questions