Pages

Sunday, 25 December 2016

Problem 78 - Palindrom

  1. import java.util.Scanner;
  2. public class palindrom
  3. {
  4.     public static boolean isPal(String s)
  5.     {  
  6.         if(s.length() == 0 || s.length() == 1)
  7.             return true;
  8.         if(s.charAt(0) == s.charAt(s.length()-1))
  9.         return isPal(s.substring(1, s.length()-1));
  10.  
  11.         return false;
  12.     }
  13.  
  14.     public static void main(String[]args)
  15.     {
  16.         Scanner scanner = new Scanner(System.in);
  17.         System.out.println("Input yang mau di check:");
  18.         String string = scanner.nextLine();
  19.  
  20.         if(isPal(string))
  21.             System.out.println(string + " Palindrom");
  22.         else
  23.             System.out.println(string + " Bukan Palindrom");
  24.     }
  25. }
Hasil output program 

0 comments:

Post a Comment