// StringPalindromeCheck.java Copyright (c) 2005 Kari Laitinen // 2005-04-10 File created. // 2005-04-10 Last modification. // A solutions to Exercise 8-5. import java.util.* ; class StringPalindromeCheck { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n Please, type in a string. \n\n " ) ; String given_string = keyboard.nextLine() ; int growing_index = 0 ; int shrinking_index = given_string.length() - 1 ; boolean given_string_is_a_palindrome = true ; while ( growing_index < shrinking_index ) { if ( given_string.charAt( growing_index ) != given_string.charAt( shrinking_index ) ) { given_string_is_a_palindrome = false ; } growing_index ++ ; shrinking_index -- ; } if ( given_string_is_a_palindrome == true ) { System.out.print( "\n The given String is a palindrome. \n " ) ; } else { System.out.print( "\n The given String is not a palindrome. \n " ) ; } // The following program lines use methods of classes String and // StringBuilder to do the above checking. System.out.print( "\n\n The same test by using standard methods ... \n" ) ; StringBuilder given_string_to_reverse = new StringBuilder( given_string ) ; String reversed_string = given_string_to_reverse.reverse().toString(); System.out.print( "\n Reversed string: " + reversed_string ) ; if ( reversed_string.equals( given_string ) ) { System.out.print( "\n\n The given String is a palindrome. \n " ) ; } else { System.out.print( "\n\n The given String is not a palindrome. \n " ) ; } } }