Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Stack<T> {
- private int stackPos;
- private T[] stack;
- public Stack() {
- this.stackPos = -1;
- this.stack = (T[]) new Object[100];
- }
- public void push(T value) {
- if (this.stackPos < this.stack.length - 1) {
- this.stack[++stackPos] = value;
- }
- }
- public T pop() {
- if (this.isEmpty()) {
- return null;
- }
- return this.stack[stackPos--];
- }
- public int length() {
- if (this.isEmpty()) {
- return 0;
- }
- return this.stackPos + 1;
- }
- public T head() {
- if (this.isEmpty()) {
- return null;
- }
- return this.stack[stackPos];
- }
- public boolean isEmpty() {
- return this.stackPos == -1;
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- Stack<Character> stack = new Stack<Character>();
- String answer = input.nextLine();
- for (int i = 0; i < answer.length(); i++) {
- stack.push(answer.charAt(i));
- }
- String reverseAnswer = "";
- while (!stack.isEmpty()) {
- reverseAnswer += stack.pop();
- }
- if (answer.equals(reverseAnswer))
- System.out.println("É um palindromo");
- else
- System.out.println("Não é um palindromo");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement