Sabtu, 23 April 2011

Palindrom

Palindrom adalah sebuah kata, frasa, angka maupun susunan lainnya yang dapat dibaca dengan sama baik dari depan maupun belakang (spasi antara huruf-huruf biasanya diperbolehkan). Kata "palindrom" berasal dari bahasa yunani  palin ("balik") dan dromos ("pacuan kuda").
Dan berikut ini ada versi jeliotnya :

import jeliot.io.*;

public class MyClass {

public static boolean isPalindrome(String word) {
    int left  = 0;                 // index of leftmost unchecked char
    int right = word.length() -1;  // index of the rightmost
 
    while (left < right) {         // continue until they reach center
        if (word.charAt(left) != word.charAt(right)) {
            return false;          // if chars are different, finished
        }
        left++;                    // move left index toward the center
        right--;                   // move right index toward the center
    }
 
    return true;                   // if finished, all chars were same
}

    public static void main() {
    if (isPalindrome("Kasur rusaK"))
        System.out.println("Palindrome");
    else
        System.out.println("Bukan Palindrome");

    }
}
Palindrom

0 comments:

Posting Komentar

ALL INFORMATION Copyright © 2011 | Template created by O Pregador | Powered by Blogger