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-10  โปรแกรมตรวจสอบคำที่ได้รับว่าเป็นพาลินโดรมหรือไม่
import java.util.Scanner;
// โปรแกรมตรวจสอบคำที่ได้รับว่าเป็นพาลินโดรมหรือไม่ 
public class Palindrome {
  public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    System.out.print("คำ = ");
    String word = kb.nextLine();
    int m = word.length(), n = m/2 - 1;
    boolean isPalindrome = true;
    for (int k=0; isPalindrome && k <= n; k++) {
      String c1 = word.substring(k, k+1);
      String c2 = word.substring(m-k-1, m-k);
      isPalindrome = c1.equalsIgnoreCase(c2);
     }
    if (!isPalindrome) {
      System.out.print("ไม่");     
    }
    System.out.println("เป็นพาลินโดรม");     
  }
}
©2009 S.Prasitjutrakul