/* ESc101 Laboratory Assignment Thursday of Week of 20/09/04 1) This program checks whether a given input string is palindrome or not. */ import javabook.*; import java.awt.*; class PalinCheck { StringBuffer inputStr; MainWindow mainWindow; InputBox inputBox; // This method takes a string input from user and checks whether // it is a palindrome or not by comparing it to it's reverse /* public void checkString() { String temp1, temp2; StringBuffer revStr; boolean equal; inputBox = new InputBox(mainWindow); temp1 = inputBox.getString("Input the string"); inputStr = new StringBuffer(temp1); revStr = inputStr.reverse(); temp2 = revStr.toString(); equal = temp1.equals(temp2); if (equal) System.out.println(" Input String is a plindrome"); else System.out.println(" Input String is not a palindrome"); } */ // Alternetive Method // This method compares string characters from start and end for // half of length of string public void checkString() { String temp; int strLength, index; boolean equal = true; inputBox = new InputBox(mainWindow); temp = inputBox.getString("Input the string"); inputStr = new StringBuffer(temp); strLength = inputStr.length(); for(index = 0; index < strLength/2; index++) { if(inputStr.charAt(index) == inputStr.charAt(strLength-index-1)) ; else { equal = false; break; } } if (equal) System.out.println(" Input String is a palindrome"); else System.out.println(" Input String is not a palindrome"); } } class soln7thur1 { public static void main(String[] args) { PalinCheck palinCheck; palinCheck = new PalinCheck(); palinCheck.checkString(); } }