Introduzione a Palindrome in Java

Si dice che una stringa o un numero sia un palindromo se rimane lo stesso anche dopo che è stato invertito. Ad esempio, "MADAM" è una stringa palindromo poiché è scritta "MADAM" anche se è invertita. Ma nel caso di "FORTUNATO", questa stringa non è palindrotica in quanto è "YKCUL" quando è invertita. Alcuni dei numeri di palindromo sono 365563, 48984, 12321, 171, 88, 90009, 343 e alcune delle stringhe di palindromo sono MADAM, MALAYALAM, LOL, DAD, MOM, C ++ & ++ C, ecc . Vediamo la logica e l'implementazione del palindromo nelle seguenti sezioni. In questo argomento, impareremo a conoscere Palindrome in Java.

La logica dietro Palindrome a Java

Per verificare se un numero è un palindromo, è possibile utilizzare il seguente algoritmo.

  • Prendi una stringa o un numero di input che deve essere verificato se è un palindromo o no.

Ad esempio, prendiamo il numero 353 come input.

  • Prendi il numero di input e copialo in una variabile temporanea

353-> temp

  • Inverti usando per, mentre o qualsiasi metodo di tua scelta.

Reversednumber: rev=353

  • Confronta il numero di input e il numero invertito.

Se sono uguali, si dice che il numero sia un numero palindromo.

Altrimenti, il numero non è un numero palindromo.

vale a dire

If(inputnum==rev)
( then palindrome )
Else not palindrome

Come testare Palindrome usando vari metodi?

Esistono diversi metodi per verificare se il numero di input specificato è un palindromo o meno.

  1. Per Loop
  2. While Loop
  3. Metodo libreria (per stringhe)

Esaminiamo ciascuno di essi in dettaglio.

1. Programma per controllare il numero del palindromo usando per il ciclo

//Java program to check whether a String is a Palindrome or not using For Loop
import java.util.*;
public class PalindromeNumberExample (
//main method
public static void main(String() args) (
int r=0 ; //reversed Integer
int rem, num; //remainder and original number
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
for( ;num != 0; num /= 10 )
(
rem = num % 10; // find the modulus of the number when divided by 10
r = r * 10 + rem;
)
//check whether the original and reversed numbers are equal
if (temp == r)
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
)
else
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
)
)
)

Uscita campione 1:

Qui, poiché 353 è lo stesso quando invertito, è considerato un palindromo.

Uscita campione 2:

Qui, poiché 234 non è lo stesso quando è invertito, non è considerato un palindromo.

2. Programma per controllare il numero del palindromo usando il ciclo While

//Java program to check whether a number is a Palindrome or not using While Loop
import java.util.*;
public class PalindromeNumberExample (
public static void main(String() args) (
int r=0, rem, num;
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
while( num != 0 )
(
rem= num % 10;
r= r * 10 + rem;
num=num/10;
)
//check whether the original and reversed numbers are equal
if (temp == r)
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
)
else
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
)
)
)

Uscita campione 1:

Uscita campione 2:

3. Programma per controllare il numero del palindromo usando il metodo Library (per stringhe)

//Java program to check whether a String is a Palindrome or not using Library method
import java.util.*;
public class PalindromeNumberExample (
//Function to check whether the string is palindrome or not
public static void PalindromeCheck(String str)
(
// reverse the input String
String rev = new StringBuffer(str).reverse().toString();
// checks whether the string is palindrome or not
if (str.equals(rev))
(
System.out.println("input string is :" + str);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are equal, "+ str +" is a palindrome");
)
else
(
System.out.println("input string is :" + str);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are not equal, "+ str +" is not a palindrome");
)
)
public static void main (String() args)
(
PalindromeCheck("MALAYALAM");
)
)

Uscita campione:

Qui, la stringa di input viene passata nel programma stesso.

Per verificare se una stringa è un palindromo, viene utilizzato anche il seguente programma.

//Java program to check whether a String is a Palindrome or not
import java.util.*;
public class PalindromeNumberExample (
public static void main(String args())
(
String st, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string that has to be checked:");
st = sc.nextLine();
int len = st.length(); //length of the string
for ( int i = len- 1; i >= 0; i-- )
rev = rev + st.charAt(i);
if (st.equals(rev))
(
System.out.println("input string is :" + st);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are equal, "+ st +" is a palindrome");
)
else
(
System.out.println("input string is :" + st);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are not equal, "+ st +" is not a palindrome");
)
)
)

Uscita campione:

Conclusione

Si dice che un numero è palindromo se rimane lo stesso anche quando è invertito. Un palindromo può essere controllato anche nelle stringhe. Alcuni dei numeri e delle stringhe del palindromo sono MOM, MALAYALAM, DAD, LOL, 232, 1331, ecc. In questo documento sono trattati diversi aspetti di Palindrome come algoritmo, metodi, implementazione, ecc.

Articoli consigliati

Questa è una guida al Palindrome di Java. Qui discutiamo come testare Palindrome usando vari metodi con l'output del campione. Puoi anche dare un'occhiata ai seguenti articoli per saperne di più -

  1. Radice quadrata in Java
  2. Numero inverso in Java
  3. StringBuffer in Java
  4. CardLayout in Java
  5. Panoramica di Palindrome in C #
  6. Invertire in JavaScript
  7. Strumenti di distribuzione Java
  8. Palindrome in C ++
  9. Radice quadrata in PHP
  10. Lavorare e primi 3 metodi Enum in C #

Categoria: