Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- static String ceasar(String text, int shift) {
- StringBuilder result = new StringBuilder(); // String is immutable
- for (int pos = 0; pos < text.length(); pos++) { // for each caharcetr position
- char c = text.charAt(pos); // get character
- if (Character.isAlphabetic(c)) { // if its a letter
- char d = Character.toUpperCase((char) (c + shift)); // shift and make upper case
- result.append(d); // add it on
- } else { //add character unchanged
- result.append(c);
- }
- }
- String str = new String(result); // make a String
- return str;
- }
- public static void main(final String[] args) {
- System.out.println(ceasar("abc", 0)); // ABC
- System.out.println(ceasar("hello, world!", 1)); // IFMMP, XPSME!
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment