Java rotating characters with a negative input -
we have been working on issue little while , cannot seem figure out. suggested post new question information. have method rotates characters in string encryption. user chooses amount of shift , direction of shift. direction 1 working great, direction 2 not. output direction 1 string abcd comes out bdfh want (the shift amount changes after each letter), direction 1 output abcd coming out zzzz
can tell why shift not working properly? have been working on 1 days , it's driving me mad! thank help.
overloaded methods:
public static string rotate(string userstring, int shiftvalue, int shiftdirection) { if (shiftdirection == 1) { return rotate(userstring, shiftvalue); } else if (shiftdirection == 2) { return rotate(userstring, -shiftvalue); } else { return "this not valid way shift message."; } } public static string rotate(string userstring, int shiftvalue) { stringbuilder encoded = new stringbuilder(); int myshift = shiftvalue % 26 + 26; (char : userstring.tochararray()) { if (character.isletter(i)) { if (character.isuppercase(i)) { encoded.append((char) ('a' + (i - 'a' + myshift) % 26 )); } else { encoded.append((char) ('a' + (i - 'a' + myshift) % 26 )); } } else { encoded.append(i); } myshift = (myshift + shiftvalue) % 26; } return encoded.tostring(); }
the irony here that, believe or not, there nothing wrong code. "abcd" should produce "zzzz" when shifted in opposite direction. see this, use "zzzz" input. shifting direction 1 produce "abcd".
to elaborate:
a shifted 1 z b shifted 2, z c shifted 3, z d shifted 4, z enjoy :)
Comments
Post a Comment