En este tutorial veremos programas para verificar si la cadena dada es Palindrome o no. A continuación se muestran las formas de hacer esto.
1) Usando la batería
2) Usando la cola
3) Uso del bucle for / while

Programa 1: Verificación Palindrome usando Stack

import java.util.Stack;
import java.util.Scanner;
class PalindromeTest {

    public static void main(String[] args) {

    	System.out.print("Enter any string:");
        Scanner in=new Scanner(System.in);
        String inputString = in.nextLine();
        Stack stack = new Stack();

        for (int i = 0; i < inputString.length(); i++) {
            stack.push(inputString.charAt(i));
        }

        String reverseString = "";

        while (!stack.isEmpty()) {
            reverseString = reverseString+stack.pop();
        }

        if (inputString.equals(reverseString))
            System.out.println("The input String is a palindrome.");
        else
            System.out.println("The input String is not a palindrome.");

    }
}

Salida 1:

Enter any string:abccba
The input String is a palindrome.

Salida 2:

Enter any string:abcdef
The input String is not a palindrome.

Programa 2: controles Palindrome usando la cola

import java.util.Queue;
import java.util.Scanner;
import java.util.LinkedList;
class PalindromeTest {

    public static void main(String[] args) {

    	System.out.print("Enter any string:");
        Scanner in=new Scanner(System.in);
        String inputString = in.nextLine();
        Queue queue = new LinkedList();

        for (int i = inputString.length()-1; i >=0; i--) {
            queue.add(inputString.charAt(i));
        }

        String reverseString = "";

        while (!queue.isEmpty()) {
            reverseString = reverseString+queue.remove();
        }
        if (inputString.equals(reverseString))
            System.out.println("The input String is a palindrome.");
        else
            System.out.println("The input String is not a palindrome.");

    }
}

Salida 1:

Enter any string:xyzzyx
xyzzyx
The input String is a palindrome.

Salida 2:

Enter any string:xyz
The input String is not a palindrome.

Programa 3: uso del bucle for / while y la función String charAt

import java.util.Scanner;
class PalindromeTest {
   public static void main(String args[])
   {
      String reverseString="";
      Scanner scanner = new Scanner(System.in);

      System.out.println("Enter a string to check if it is a palindrome:");
      String inputString = scanner.nextLine();

      int length = inputString.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverseString = reverseString + inputString.charAt(i);

      if (inputString.equals(reverseString))
         System.out.println("Input string is a palindrome.");
      else
         System.out.println("Input string is not a palindrome.");

   }
}

Salida 1:

Enter a string to check if it is a palindrome:
aabbaa
Input string is a palindrome.

Salida 2:

Enter a string to check if it is a palindrome:
aaabbb
Input string is not a palindrome.

Si quieres usar Mientras bucle en el programa anterior, reemplace el bucle for con este código:

int i = length-1;
while ( i >= 0){
    reverseString = reverseString + inputString.charAt(i);
    i--;
}
leer  Programa Java para sumar dos números complejos

Por avivcas

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *