waltermilner

Untitled

Mar 24th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. public class Test {
  2.  
  3.   static String ceasar(String text, int shift) {
  4.     StringBuilder result = new StringBuilder(); // String is immutable
  5.     for (int pos = 0; pos < text.length(); pos++) { // for each caharcetr position
  6.       char c = text.charAt(pos); // get character
  7.       if (Character.isAlphabetic(c)) { // if its a letter
  8.         char d = Character.toUpperCase((char) (c + shift)); // shift and make upper case
  9.         result.append(d); // add it on
  10.  
  11.       } else { //add character unchanged
  12.         result.append(c);
  13.       }
  14.     }
  15.     String str = new String(result); // make a String
  16.     return str;
  17.   }
  18.  
  19.   public static void main(final String[] args) {
  20.     System.out.println(ceasar("abc", 0)); // ABC
  21.     System.out.println(ceasar("hello, world!", 1)); // IFMMP, XPSME!
  22.   }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment