Manusia Biasa …

2 February 2009

Palindrom

Filed under: Belajar dan Tutorial,Teknologi dan Komputer — rachlax @ 4:43 pm
Tags: , ,

script java dibawah hanya untuk mengecek apakah sebuah/serangkaian kata termasuk palindrom (jika dibaca dari depan atau belakang sama).
Sebelum mencobanya di-compile dulu : javac Palindrom.java
Untuk mencobanya silakan jalankan : java Palindrom “kasur rusak”

silakan mencoba…🙂



/**
Filename : Palindrom.java
Author   : Rachmad Laksana
*/


public class Palindrom {
   private static String kata;
   private static int panjangKata, awal, akhir;
   private static boolean genapKah;

   public static void main(String[]args) {
      kata = args[0];
      awal = 0;
      panjangKata = kata.length();
      akhir = panjangKata - 1;
      genapKah = (panjangKata % 2 == 0);

      if(genapKah) {
         
         
         //System.out.println("Genap");
         
         
         while(kata.charAt(awal) == kata.charAt(akhir) && awal < akhir) {
            awal++;
            akhir--;
         }

         if(akhir < awal) { System.out.println("kata : " + kata + " -> Palindrom"); }
         else { System.out.println("kata : " + kata + " -> bukan Palindrom"); }
      }
      else {
         
         
         //System.out.println("Ganjil");
         
         
         while(kata.charAt(awal) == kata.charAt(akhir) && awal != akhir) {
            awal++;
            akhir--;
         }

         if(akhir == awal) { System.out.println("kata : " + kata + " -> Palindrom"); }
         else { System.out.println("kata : " + kata + " -> bukan Palindrom"); }
      }
   }
}




Output :
kata : kasur rusak -> Palindrom

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: