Comment vérifier si une chaîne est un palindrome

Comment vérifier si une chaîne est un palindrome

Une chaîne est dite palindrome si la chaîne d'origine et son inverse sont identiques. Dans cet article, vous découvrirez l'algorithme permettant de déterminer si la chaîne donnée est un palindrome ou non. Vous apprendrez également à implémenter cet algorithme dans les langages de programmation les plus populaires tels que C++, Python, C et JavaScript.





Exemples de chaîne palindrome

Voici quelques exemples de chaînes palindromes et non palindromes :





Algorithme pour déterminer si une chaîne donnée est un palindrome ou non

Les algorithmes sont simplement une série d'instructions qui sont suivies, étape par étape, pour faire quelque chose d'utile ou résoudre un problème. Vous pouvez résoudre le problème du palindrome de chaîne en utilisant l'algorithme ci-dessous :





  1. Déclarez une fonction qui accepte la chaîne donnée en tant que paramètre.
  2. Créez une variable booléenne et définissez-la sur true. Soit la variable drapeau .
  3. Trouver la longueur de la chaîne donnée. Soit la longueur m .
  4. Convertit la chaîne donnée en minuscule pour rendre la comparaison entre les caractères insensible à la casse.
  5. Initialiser la variable d'indice faible comme meugler et le mettre à 0.
  6. Initialiser la variable d'indice élevé comme haute et réglez-le sur n-1.
  7. Effectuez les opérations suivantes lorsque le niveau bas est inférieur au niveau élevé :
    • Comparez les caractères à indice faible et indice élevé.
    • Si les caractères ne correspondent pas, définissez l'indicateur sur false et interrompez la boucle.
    • Incrémentez la valeur de bas de 1 et décrémentez la valeur de haut de 1.
  8. Si le drapeau est vrai à la fin de la fonction, cela signifie que la chaîne donnée est un palindrome.
  9. Si le drapeau est faux à la fin de la fonction, cela signifie que la chaîne donnée n'est pas un palindrome.

Programme C++ pour vérifier si une chaîne donnée est un palindrome ou non

Vous trouverez ci-dessous l'implémentation C++ pour déterminer si la chaîne donnée est un palindrome ou non :

comment regarder la télévision sur un ordinateur portable avec internet
// 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;
}

Sortir:



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

Programme Python pour vérifier si une chaîne donnée est un palindrome ou non

Vous trouverez ci-dessous l'implémentation Python pour déterminer si la chaîne donnée est un palindrome ou non :

# 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)

Sortir:





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

C Programme pour vérifier si une chaîne donnée est un palindrome ou non

Vous trouverez ci-dessous l'implémentation C pour déterminer si la chaîne donnée est un palindrome ou non :

// 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;
}

Sortir:





sites de téléchargement de logiciels version complète gratuite
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

Programme JavaScript pour vérifier si une chaîne donnée est un palindrome ou non

Vous trouverez ci-dessous l'implémentation JavaScript pour déterminer si la chaîne donnée est un palindrome ou non :

// 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);

Sortir:

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

Apprenez à gérer les chaînes en programmation

Travailler avec des chaînes fait partie intégrante de la programmation. Vous devez savoir comment utiliser et manipuler des chaînes dans l'un des langages de programmation comme Python, JavaScript, C++, etc.

Si vous cherchez un langage pour commencer, Python est un excellent choix.

Partager Partager Tweeter E-mail Apprendre Python ? Voici comment manipuler des chaînes

L'utilisation et la manipulation de chaînes en Python peuvent sembler difficiles, mais elles sont trompeusement simples.

Lire la suite
Rubriques connexes
  • La programmation
  • Tutoriels de codage
A propos de l'auteur Yuvraj Chandra(60 articles publiés)

Yuvraj est un étudiant de premier cycle en informatique à l'Université de Delhi, en Inde. Il est passionné par le développement Web Full Stack. Quand il n'écrit pas, il explore la profondeur de différentes technologies.

Plus de Yuvraj Chandra

Abonnez-vous à notre newsletter

Rejoignez notre newsletter pour des conseils techniques, des critiques, des ebooks gratuits et des offres exclusives !

Cliquez ici pour vous abonner