Es diu que una corda és un palíndrom si la corda original i el seu revers són els mateixos. En aquest article, aprendreu sobre l'algorisme per determinar si la cadena donada és un palíndrom o no. També aprendreu a implementar aquest algorisme en els llenguatges de programació més populars com C ++, Python, C i JavaScript.
Exemples de corda de palíndrom
A continuació es mostren alguns exemples de cordes de palindrom i no palindrom:
Algorisme per determinar si una cadena determinada és un palíndrom o no
Els algoritmes són simplement una sèrie d’instruccions que es segueixen, pas a pas, per fer alguna cosa útil o resoldre un problema. Podeu resoldre el problema del palíndrom de cadenes mitjançant l’algoritme següent:
- Declareu una funció que accepta la cadena donada com a paràmetre.
- Creeu una variable booleana i configureu-la a true. Deixeu que la variable sigui bandera .
- Cerqueu la longitud de la cadena donada. Deixeu que la longitud sigui n .
- Convertiu la cadena donada en minúscula per fer que la comparació entre els caràcters distingeixi entre majúscules i minúscules.
- Inicialitzeu la variable d’índex baix com a baix i establiu-lo a 0.
- Inicialitzeu la variable d'índex alt com a alt i configureu-lo a n-1.
- Feu el següent, mentre que el mínim és inferior a l’alt:
- Compareu els caràcters amb índex baix i índex alt.
- Si els caràcters no coincideixen, configureu la bandera com a falsa i trenqueu el bucle.
- Incrementeu el valor de mínim en 1 i disminuïu el valor de mínim en 1.
- Si el senyalador és cert al final de la funció, significa que la cadena donada és un palíndrom.
- Si el senyalador és fals al final de la funció, significa que la cadena donada no és un palíndrom.
Programa C ++ per comprovar si una cadena determinada és un palíndrom o no
A continuació es mostra la implementació de C ++ per determinar si la cadena donada és un palíndrom o no:
com funciona la targeta SD wifi
// 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;
}
Sortida:
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
Programa Python per comprovar si una cadena determinada és un palíndrom o no
A continuació es mostra la implementació de Python per determinar si la cadena donada és un palíndrom o no:
# 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)
Sortida:
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
Programa C per comprovar si una cadena determinada és un palíndrom o no
A continuació es mostra la implementació C per determinar si la cadena donada és un palíndrom o no:
// 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;
}
Sortida:
dispositiu USB desconegut (ha fallat la sol·licitud del descriptor del dispositiu) Windows 10
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
Programa JavaScript per comprovar si una cadena determinada és un palíndrom o no
A continuació es mostra la implementació de JavaScript per determinar si la cadena donada és un palíndrom o no:
// 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);
Sortida:
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
Apreneu a tractar les cadenes de programació
Treballar amb cadenes és una part integral de la programació. Heu de saber utilitzar i manipular cadenes en qualsevol dels llenguatges de programació com Python, JavaScript, C ++, etc.
Si busqueu un idioma per començar, Python és una opció excel·lent.
Compartir Compartir Tweet Correu electrònic Aprèn Python? A continuació s’explica com es manipulen les cordesL’ús i la manipulació de cadenes a Python poden semblar difícils, però és enganyosament senzill.
Llegiu a continuació Temes relacionats- Programació
- Tutorials de codificació
Yuvraj és estudiant universitari en ciències de la computació a la Universitat de Delhi, Índia. És un apassionat del desenvolupament web Full Stack. Quan no escriu, explora la profunditat de les diferents tecnologies.
Més de Yuvraj ChandraSubscriu-te al nostre butlletí
Uniu-vos al nostre butlletí per obtenir consells tècnics, ressenyes, llibres electrònics gratuïts i ofertes exclusives.
Feu clic aquí per subscriure-us