Friday, April 22, 2011

Palindrome Finder

CHANGE THE CODE A LITTLE SO I DONT GET IN TROUBLE
public class Lab14
{
 public static void main(String args[])
 {
  System.out.println("100 POINT VERSION,");
  boolean finished = false;
  do
  {
   System.out.println("===============================================================");
   System.out.print("Enter a string ===>> ");
   Palindrome p = new Palindrome(Expo.enterString());
   p.displayData();
   System.out.print("Do you wish to repeat this program [Y/N]? ===>> ");
   char repeat = Expo.enterChar();
   finished = (repeat != 'Y' && repeat != 'y');
   System.out.println();
  }
  while (!finished);
 }
}
class Palindrome
{
 private String str;
 private String str2 = "";
 private int d = 0;
 public Palindrome(String s)
 {
  str = s;
 }
 public boolean isPal()
 {
  int s = str.length() - 1;
  int k = (s/2);
  for (int i = s; i >= 0; i--)
  {
   str2 += str.charAt(i);
  }
  if(str.equals(str2)){
   d = 1;
   return true;
  }
  else{
  return false;
  }
 }
 public boolean almostPal()
 {
  purge();
  if(d == 1){
   return false;
  }
  else if(str.equals(str2)){
   return true;
  }
  else{
  return false;
  }
 }
 public String purge()
 {
  str = str.replace(" ","");
  str2 = str2.replace(" ","");
  str = str.replace(",","");
  str2 = str2.replace(",","");
  str = str.replace("'","");
  str2 = str2.replace("'","");
  str = str.replace(".","");
  str2 = str2.replace(".","");
  str = str.replace("!","");
  str2 = str2.replace("!","");
  str = str.toUpperCase();
  str2 = str2.toUpperCase();
  return "";
 }
 public void displayData()
 {
  System.out.println();
  System.out.println("String:        " + str);
  System.out.println("Palindrome:      " + isPal());
  System.out.println("Almost Palindrome:    " + almostPal());
  System.out.println("===============================================================");
  System.out.println();
  d = 0;
 }
}

No comments:

Post a Comment