encryption - Caesar cipher in Java with recursion? -


so, i'm trying come way encryption/decryption of caesar cipher (where take in value, shift letters many times along alphabet, example, if key 3, become d, b e, , c f , on) need use recursion, rather iteration. here's have far, it's encrypting last character, , outputting "encrypted - w" doesn't make sense me.

public class driver {  static string encrypted = "";  public static void main(string[] args) {      system.out.println("encrypted - " + cipher("encrypt", 3));  }  public static string cipher(string str, int i){     char ch = str.charat(0);     stringbuffer output = new stringbuffer();     if (str.length() <= 1) {             ch = (char) ('a' + (ch - 'a' + i) %26);             output.append(ch);             return output.tostring();      }     else{         return cipher(str.substring(1),i);      }   }  } 

hey in logic doing encryption last letter only. have letters. , @thilo rightly said have take care of head also. working solution :

public static string cipher(string str, int i) {     char ch = str.charat(0);     stringbuffer output = new stringbuffer();     ch = (char) ('a' + (ch - 'a' + i) % 26);     output.append(ch);      if (str.length() > 1) {         output.append(cipher(str.substring(1), i));     }      return output.tostring(); } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -