Come verificare se una stringa è un palindromo

Come verificare se una stringa è un palindromo

Una stringa si dice palindroma se la stringa originale e il suo contrario coincidono. In questo articolo imparerai a conoscere l'algoritmo per determinare se la stringa data è un palindromo o meno. Imparerai anche come implementare questo algoritmo nei linguaggi di programmazione più popolari come C++, Python, C e JavaScript.





Esempi di corde palindromiche

Di seguito sono riportati alcuni esempi di stringhe palindrome e non palindrome:





Algoritmo per determinare se una data stringa è un palindromo o no

Gli algoritmi sono semplicemente una serie di istruzioni che vengono seguite, passo dopo passo, per fare qualcosa di utile o risolvere un problema. Puoi risolvere il problema del palindromo delle stringhe usando l'algoritmo seguente:





  1. Dichiarare una funzione che accetta la stringa data come parametro.
  2. Crea una variabile booleana e impostala su true. Sia la variabile bandiera .
  3. Trova la lunghezza della stringa data. Lascia che sia la lunghezza n .
  4. Converti la stringa data in minuscolo per rendere il confronto tra i caratteri senza distinzione tra maiuscole e minuscole.
  5. Inizializza la variabile di indice basso come basso e impostalo su 0.
  6. Inizializza la variabile ad alto indice come alto e impostalo su n-1.
  7. Effettuare le seguenti operazioni mentre il valore basso è inferiore a quello alto:
    • Confronta i caratteri a indice basso e indice alto.
    • Se i caratteri non corrispondono, imposta il flag su false e interrompi il ciclo.
    • Incrementa il valore di low di 1 e decrementa il valore di high di 1.
  8. Se il flag è vero alla fine della funzione, significa che la stringa data è un palindromo.
  9. Se il flag è falso alla fine della funzione, significa che la stringa data non è palindroma.

Programma C++ per verificare se una data stringa è palindroma o no

Di seguito è riportata l'implementazione C++ per determinare se la stringa data è palindroma o meno:

puoi cambiare il tuo nome utente snapchat?
// Including libraries
#include
using namespace std;
// Function to check string palindrome
void checkPalindrome(string str)
{
// Flag to check if the given string is a palindrome
bool flag = true;

// Finding the length of the string
int n = str.length();

// Converting the string to lowercase
for(int i = 0; i {
str[i] = tolower(str[i]);
}

// Initializing low index variable
int low = 0;

// Initializing high index variable
int high = n-1;

// Running the loop until high is greater than low
while (high > low)
{
// If the characters are not same, set the flag to false
// and break from the loop
if(str[high] != str[low])
{
flag = false;
break;
}

// Increment the low index variable
low++;

// Decrement the high index variable
high--;
}

// Check if flag is true or false
if (flag)
{
cout << 'Yes, the given string is a palindrome' << endl;
}
else
{
cout << 'No, the given string is not a palindrome' << endl;
}

return;

}
int main()
{
// Test case: 1
string str1 = 'MUO';
checkPalindrome(str1);

// Test case: 2
string str2 = 'madam';
checkPalindrome(str2);

// Test case: 3
string str3 = 'MAKEUSEOF';
checkPalindrome(str3);

// Test case: 4
string str4 = 'racecar';
checkPalindrome(str4);

// Test case: 5
string str5 = 'mom';
checkPalindrome(str5);

return 0;
}

Produzione:



No, the given string is not a palindrome
Yes, the given string is a palindrome
No, the given string is not a palindrome
Yes, the given string is a palindrome
Yes, the given string is a palindrome

Programma Python per verificare se una determinata stringa è un palindromo o meno

Di seguito è riportata l'implementazione di Python per determinare se la stringa data è palindroma o meno:

# Function to check string palindrome
def checkPalindrome(str):
# Flag to check if the given string is a palindrome
flag = True
# Finding the length of the string
n = len(str)
# Converting the string to lowercase
str = str.lower()
# Initializing low index variable
low = 0
# Initializing high index variable
high = n-1
# Running the loop until high is greater than low
while high > low:
# If the characters are not same, set the flag to false
# and break from the loop
if str[high] != str[low]:
flag = False
break
# Increment the low index variable
low = low + 1
# Decrement the high index variable
high = high - 1
# Check if flag is true or false
if flag:
print('Yes, the given string is a palindrome')
else:
print('No, the given string is not a palindrome')
# Test case: 1
str1 = 'MUO'
checkPalindrome(str1)
# Test case: 2
str2 = 'madam'
checkPalindrome(str2)
# Test case: 3
str3 = 'MAKEUSEOF'
checkPalindrome(str3)
# Test case: 4
str4 = 'racecar'
checkPalindrome(str4)
# Test case: 5
str5 = 'mom'
checkPalindrome(str5)

Produzione:





No, the given string is not a palindrome
Yes, the given string is a palindrome
No, the given string is not a palindrome
Yes, the given string is a palindrome
Yes, the given string is a palindrome

Programma C per verificare se una data stringa è un palindromo o meno

Di seguito è riportata l'implementazione C per determinare se la stringa data è palindroma o meno:

// Including libraries
#include
#include
#include
#include
// Function to check string palindrome
void checkPalindrome(char str[])
{
// Flag to check if the given string is a palindrome
bool flag = true;
// Finding the length of the string
int n = strlen(str);
// Converting the string to lowercase
for(int i = 0; i {
str[i] = tolower(str[i]);
}
// Initializing low index variable
int low = 0;
// Initializing high index variable
int high = n-1;
// Running the loop until high is greater than low
while (high > low)
{
// If the characters are not same, set the flag to false
// and break from the loop
if(str[high] != str[low])
{
flag = false;
break;
}
// Increment the low index variable
low++;
// Decrement the high index variable
high--;
}
// Check if flag is true or false
if (flag)
{
printf('Yes, the given string is a palindrome ⁠n');
}
else
{
printf('No, the given string is not a palindrome ⁠n');
}
return;
}
int main()
{
// Test case: 1
char str1[] = 'MUO';
checkPalindrome(str1);
// Test case: 2
char str2[] = 'madam';
checkPalindrome(str2);
// Test case: 3
char str3[] = 'MAKEUSEOF';
checkPalindrome(str3);
// Test case: 4
char str4[] = 'racecar';
checkPalindrome(str4);
// Test case: 5
char str5[] = 'mom';
checkPalindrome(str5);
return 0;
}

Produzione:





come controllare iphone per virus
No, the given string is not a palindrome
Yes, the given string is a palindrome
No, the given string is not a palindrome
Yes, the given string is a palindrome
Yes, the given string is a palindrome

Programma JavaScript per verificare se una determinata stringa è palindroma o no

Di seguito è riportata l'implementazione JavaScript per determinare se la stringa data è palindroma o meno:

// Function to check string palindrome
function checkPalindrome(str) {
// Flag to check if the given string is a palindrome
var flag = true;
// Finding the length of the string
var n = str.length;
// Converting the string to lowercase
str = str.toLowerCase();
// Initializing low index variable
var low = 0;
// Initializing high index variable
var high = n-1;
// Running the loop until high is greater than low
while (high > low) {
// If the characters are not same, set the flag to false
// and break from the loop
if(str[high] != str[low]) {
flag = false;
break;
}
// Increment the low index variable
low++;
// Decrement the high index variable
high--;
}
// Check if flag is true or false
if (flag) {
console.log('Yes, the given string is a palindrome');
} else {
console.log('No, the given string is not a palindrome');
}
}
// Test case: 1
var str1 = 'MUO';
checkPalindrome(str1);
// Test case: 2
var str2 = 'madam';
checkPalindrome(str2);
// Test case: 3
var str3 = 'MAKEUSEOF';
checkPalindrome(str3);
// Test case: 4
var str4 = 'racecar';
checkPalindrome(str4);
// Test case: 5
var str5 = 'mom';
checkPalindrome(str5);

Produzione:

No, the given string is not a palindrome
Yes, the given string is a palindrome
No, the given string is not a palindrome
Yes, the given string is a palindrome
Yes, the given string is a palindrome

Impara come gestire le stringhe nella programmazione

Lavorare con le stringhe è parte integrante della programmazione. Devi sapere come usare e manipolare le stringhe in uno qualsiasi dei linguaggi di programmazione come Python, JavaScript, C++, ecc.

Se stai cercando un linguaggio con cui iniziare, Python è una scelta eccellente.

Condividere Condividere Tweet E-mail Imparare Python? Ecco come manipolare le stringhe

Usare e manipolare le stringhe in Python può sembrare difficile, ma è ingannevolmente semplice.

Leggi Avanti
Argomenti correlati
  • Programmazione
  • Tutorial sulla programmazione
Circa l'autore Yuvraj Chandra(60 articoli pubblicati)

Yuvraj è uno studente universitario di Informatica presso l'Università di Delhi, in India. È appassionato di sviluppo Web Full Stack. Quando non scrive, esplora la profondità di diverse tecnologie.

Altro da Yuvraj Chandra

Iscriviti alla nostra Newsletter

Iscriviti alla nostra newsletter per suggerimenti tecnici, recensioni, ebook gratuiti e offerte esclusive!

Clicca qui per iscriverti