0

I've been trying for quite a while to be able to encrypt a string a certain way with the changing of the characters, I have managed to do that part by finding the rot13 :). However I'm quite confused on how to store the result as another String so that it can be viewed later.

public static void main(String[] args) {

    String s = "example string";
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if       (c >= 'a' && c <= 'o') c += 6;
        else if  (c >= 'A' && c <= 'O') c += 6;
        else if  (c >= 'p' && c <= 'z') c -= 10;
        else if  (c >= 'P' && c <= 'Z') c -= 10;
        System.out.print(c);

    }

}

Is there possibly a way to store each individual value of char c into separate strings and then join the strings together? I've tired using the StringBuilder but have had no luck so far, Could anyone point me in the right direction? Thanks :)

EDIT

Yeah StringBuilder was indeed the way to go, thanks anyway :)

aluckii
  • 39
  • 2
  • 9
  • 3
    Use a ``StringBuilder`, `append` each character to the end of it and use `toString` to get the resulting `String` value, once you've finished creating it... – MadProgrammer Mar 25 '14 at 00:40
  • StringBuilder is the way to go here. Can you post code you're tried with StringBuilder? – Ivan Nevostruev Mar 25 '14 at 00:40
  • What happened when you used StringBuilder? I am assuming that you appended 'c' to it. – chauhraj Mar 25 '14 at 00:41
  • possible duplicate of [ROT-13 function in java?](http://stackoverflow.com/questions/8981296/rot-13-function-in-java) – Bae Mar 25 '14 at 00:56
  • Hi yeah thanks for the help StringBuilder was the way to go, i must have made a serious error last time, thanks for help anyway people :) – aluckii Mar 28 '14 at 12:44

1 Answers1

0
char a = 'a';
char b = 'b';
String str = "" + a+b;

or you could try StringBuilder's append.

sager89
  • 940
  • 1
  • 12
  • 25