Rotation13 Encryption of an Array

Hello,
Ive implemented this code for ROT13, but problem i am facing is the lower half of the array,i.e the last 13 characters (n to z & N to Z) are not rotated,instead some garbage value gets printed.

Ive attached the Serial output monitor below,please check.

Regards.

char array[27]="abcdefghijklmnopqrstuvwxyz";
char inByte;
int i=0;
void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);   // initialize the serial port at 9600 baud


  } /* setup */

void loop() {

          inByte = array[i];
         
          if (inByte >= 'A' && inByte <= 'M')
         {
           
          for( i=0; i<26;i++){
          array[i]=array[i] + 13;
          
          Serial.print(array[i]);
         }
         
         }
         
         else if (inByte >= 'N' && inByte <= 'Z')
         {
           
          for( i=0; i<26;i++){
          array[i]=array[i] + 13;
          Serial.print(array[i]);
         }
         
         }
         
        else if (inByte >= 'a' && inByte <= 'm')
         {
           
          for( i=0; i<26;i++){
          array[i]=array[i] + 13;
          Serial.print(array[i]);
         }
         
         }
         
        
         
        else if (inByte >= 'n' && inByte <= 'z')
         {
           
          for( i=0; i<26;i++){
          array[i]=array[i] + 13;
          Serial.print(array[i]);
         }
         
         } 
}

Ok, so the Array has 26 elements in it.
You Loop with i from 0 to 25.
at some Point, i = 13.
and you replace the character at Array Position 13 with the character at Array Position 26.

...
Oops, the Array only has a Maximum of 25 (0-25 = 26 characters). So, you are putting garbage
into Position 13.
Then you continue to put garbage into all positions 13 through 25.

Woah JaBa ,i never thought of that. Brilliant!!

Now the code runs appropriately.

char array[27]="abcdefghijklmnopqrstuvwxyz";
char inByte;
int i=0;
void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);   // initialize the serial port at 9600 baud


  } /* setup */

void loop() {

          inByte = array[i];
          
          if (inByte >= 'A' && inByte <= 'M')
         {
           
          for( i=0; i<13;i++){
          array[i]=array[i] + 13;
          
          Serial.print(array[i]);
         }
         
         }
         
        
         
        else if (inByte >= 'a' && inByte <= 'm')
         {
           
          for( i=0; i<13;i++){
          array[i]=array[i] + 13;
          Serial.print(array[i]);
         }
         
         }
         
          else if (inByte >= 'N' && inByte <= 'Z')
         {
           
          for( i=13; i<27;i++){
          array[i]=array[i] - 13;
          Serial.print(array[i]);
         }
         
         }
         
        
         
        else if (inByte >= 'n' && inByte <= 'z')
         {
           
          for( i=13; i<27;i++){
          array[i]=array[i] - 13;
          Serial.print(array[i]);
         }
         
         } 
         
}

Ive successfully coded my arduino to read the text from PS2 Keyboard, and when pressed TAB key of PS2 keyboard,arduino encrypts the text using the famous ROT13 cipher.

But there are some bugs in the program, can someone please point them out?
When the ciphered text is printed on LCD, it also prints some garbage value. Ive attached the output of LCD,please check.
nopqr is what i type, it does encrypt the text successfully when i hit tab,but why the garbage values at the end?

Also,a big Sorry to moderators,for creating numerous threads for same topic. All threads had some different problems though.

Thank You. :slight_smile:

#include <LiquidCrystal.h>
#include <PS2Keyboard.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 13, 14, 15, 16, 17);

const int DataPin = 2;
const int IRQpin =  3;


PS2Keyboard keyboard;

char KbChars;
char array[5];
char inByte;
int i=0;

void setup() {

  lcd.begin(16, 2);
  delay(100);
  keyboard.begin(DataPin, IRQpin);
  lcd.setCursor(0, 0);
 } 


void loop() {
   if (keyboard.available()) {
  KbChars=keyboard.read();
  array[i]=KbChars;
  i++;
  
  
  if (KbChars == PS2_TAB) {
      lcd.setCursor(0, 1);
      rot13();
      lcd.println(array);
    }
    lcd.print(KbChars);
  }
}   

void rot13() {

         for (int i = 0; i <= 5; ++i) {  
            inByte = array[i];
       if (!inByte) return; //end of string reached
          
        if (inByte >= 'A' && inByte <= 'M')
            inByte +=13;
        else if (inByte >= 'a' && inByte <= 'm')
            inByte +=13;
        else if (inByte >= 'N' && inByte <= 'Z')
            inByte -=13;
        else if (inByte >= 'n' && inByte <= 'z')
            inByte -=13;

        array[i] = inByte;
     } 
    }

char r13(char ch) {
  if(ch >= 'A' && ch <= 'Z') return 'A' + (ch-'A'+13) % 26;  
  else if(ch >= 'a' && ch <= 'z') return 'a' + (ch-'a'+13) % 26;
  else return ch;
}