5. ข้อความ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 5-11  โปรแกรมเข้ารหัส ROT-13
import java.util.Scanner;
// โปรแกรมเข้าและถอดรหัสด้วยวิธี ROT-13
public class Rot13 {
  public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    System.out.print("ข้อความ = ");
    String text = kb.nextLine();
    String upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String lowerCase = "abcdefghijklmnopqrstuvwxyz";
    String rot13 = "";
    int n = text.length();
    for (int k=0; k<n; k++) {
      String c = text.substring(k, k + 1);
      int j = upperCase.indexOf(c);
      if (j >= 0) {
        int i = (j + 13) % upperCase.length();
        rot13 = rot13 + upperCase.substring(i, i + 1);
      } else {
        j = lowerCase.indexOf(c);
        if (j >= 0) {
          int i = (j + 13) % lowerCase.length();
          rot13 = rot13 + lowerCase.substring(i, i + 1);
        } else {
          rot13 = rot13 + c;
        }
      }
    }
    System.out.println("ROT-13 = " + rot13);
  }
}
©2009 S.Prasitjutrakul