Advertisement
Shailrshah

Palindrome Test

Aug 24th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. import java.io.*;
  2. class Palindrome{
  3.     private static String getInput(String prompt){
  4.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  5.         System.out.print(prompt);
  6.         System.out.flush();
  7.         try{
  8.             return stdin.readLine();
  9.         }catch(Exception e){
  10.             return "Error: " + e.getMessage();
  11.         }
  12.     }
  13.     static public void main (String[] args){
  14.         String input = getInput("Enter a string: ");
  15.         int length = input.length();
  16.         for(int i=0; i<length/2; i++){
  17.             if(input.charAt(i) != input.charAt(length - 1 - i)){
  18.                 System.out.println("It's not a Palindrome!");
  19.                 return;
  20.             }
  21.         }
  22.         System.out.print("It's a Palindrome!");
  23.     }
  24. }
  25.  
  26. // Output:-
  27. // C:\Java>java Palindrome
  28. // Enter a string: dad
  29. // It's a Palindrome!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement