Advertisement
Guest User

Ping He

a guest
Apr 3rd, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. import java.util.Stack;
  2.  
  3. public class Stack1 {
  4.    
  5.     public static void main(String[] args) {
  6.         String test = "tacocat";
  7.         if(isPalidrome(test)){
  8.             System.out.println("yes");
  9.         }else{
  10.             System.out.println("no");
  11.         }
  12.     }
  13.    
  14.     public static boolean isPalidrome(String test) {
  15.         int length = test.length();
  16.         int x=0;
  17.         Stack <Character> stack = new Stack<Character>();
  18.         for(int i = 0; i < length/2; i++) {
  19.           stack.push(test.charAt(i));
  20.             if(length % 2 != 0 && i==length/2 + 1) {
  21.                 break;
  22.             }
  23.         if(stack.empty()){
  24.             return false
  25.         }
  26.         while(i < length) {
  27.             char test = stack.pop();
  28.             if(fromStack != test.charAt(i)) {
  29.                 return false;
  30.             }
  31.         }
  32.         return true;
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement