Advertisement
Guest User

palindromes.java

a guest
Sep 11th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.text.*;
  2. import javax.swing.*;
  3. import java.util.*;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6.  
  7. public class palindromes {
  8.     public static void main(String[] args) {
  9.         Scanner infile = new Scanner (System.in);
  10.         String s = "";
  11.         int StrLength;
  12.         while (!s.equals("Q"))
  13.         {
  14. //          Explains what a palindrome is and asks user to enter test phrase.
  15.                 System.out.println("This program will test strings to see if they are palindromes.");
  16.             System.out.println("A palindrome is a word or phrase that is spelled the same way frontwards and backwards.");
  17.             System.out.println("Please enter a string to be tested (Enter 'Q' to quit):  ");
  18.  
  19. //          User enters test phrase.  If phrase == 'Q', program ends.
  20.                 s = infile.next();
  21.  
  22. //          Converts all letters to caps, removes all white-space and punctuation,
  23. //                           initializes and stores the value of the length of String 2, initializes the stack.
  24.                                  s.toUpperCase();
  25.             s.replaceAll("\\W", "");
  26.             s.replaceAll("\\s", "");
  27.             StrLength = s.length() - 1;
  28.             Stk s2 = new Stk(s.length());
  29.  
  30.             int i;
  31.  
  32. //          Pushes every letter in the string into the stack.
  33.                 for (i = 0; i <= StrLength; i++) {
  34.                     s2.push(s.charAt(i));
  35.                 };
  36.             char c;
  37.             i = 0;
  38.  
  39. //          While S3 is not empty, compare popped character with character in same position in string.
  40. //              If both are equal, continue until s3 is empty.  If there is any difference, end the loop.
  41.                 while (!s2.isEmpty()) {
  42.                     c = s2.pop();
  43.                     if (c == s.charAt(i)) {
  44.                         i++;
  45.                         if (i > StrLength) {
  46.                             System.out.println("This string is a palindrome.  Please enter another string to test (Enter Q to quit).");
  47.                             s = infile.next();
  48.                             StrLength = 0;
  49.                         };
  50.                     }
  51.                     else
  52.                     {
  53.                         System.out.println("This string is not a palindrome.  Please enter another string to test (Enter Q to quit).");
  54.                         s = infile.next();
  55.                         StrLength = 0;
  56.                     };
  57.  
  58.                 };
  59.         };  
  60.  
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement