java - Define a method named encode that accepts a String as input and returns a new String which is the original String encoded using the ROT13 cipher -
for example: string "abc" should return "nop".
i did system.out.println , printing correct result, however, junit test case giving me red bar. method move13 takes character , moves 13 spaces left or right.
the method encode having trouble with.
package code; public class encoder { public char move13(char letter) { if (letter >= 'a' && letter <= 'm') { return (char)(letter + 13); } if (letter >= 'a' && letter <= 'm') { return (char)(letter + 13); } if (letter >= 'n' && letter <= 'z') { return (char)(letter - 13); } if (letter >= 'n' && letter <= 'z') { return (char)(letter - 13); } return letter; } public string encode(string text) { string valueofchar = ""; (int = 0; < text.length(); i++) { char character = text.charat(i); character = move13(character); valueofchar = string.valueof(character); system.out.println(valueofchar); } return valueofchar; } }
here go, hope helps
public static char move13(char letter) { if (letter >= 'a' && letter <= 'm') return (char) (letter + 13); if (letter >= 'a' && letter <= 'm') return (char) (letter + 13); if (letter >= 'n' && letter <= 'z') return (char) (letter - 13); if (letter >= 'n' && letter <= 'z') return (char) (letter - 13); return letter; } public static string encode(string text) { stringbuilder sb = new stringbuilder(); (int = 0; < text.length(); i++) { char character = text.charat(i); character = move13(character); sb.append(character); //system.out.println(valueofchar); } return sb.tostring(); }
the char concatenated in stringbuilder (sb.append(char))
. after each letter processed in move13()
, return concatenated chars (sb.tostring()
).
Comments
Post a Comment