Hoe te controleren of een string een palindroom is?

Hoe te controleren of een string een palindroom is?

Een string wordt een palindroom genoemd als de originele string en het omgekeerde hetzelfde zijn. In dit artikel leer je over het algoritme om te bepalen of de gegeven string een palindroom is of niet. Je leert ook hoe je dit algoritme implementeert in de meest populaire programmeertalen zoals C++, Python, C en JavaScript.





Voorbeelden van Palindroom String

Hieronder staan ​​enkele voorbeelden van palindroom- en niet-palindroomstrings:





Algoritme om te bepalen of een gegeven string een palindroom is of niet

Algoritmen zijn gewoon een reeks instructies die stap voor stap worden gevolgd om iets nuttigs te doen of een probleem op te lossen. U kunt het string-palindroomprobleem oplossen met behulp van het onderstaande algoritme:





  1. Declareer een functie die de gegeven string als parameter accepteert.
  2. Maak een booleaanse variabele en stel deze in op true. Laat de variabele zijn vlag .
  3. Zoek de lengte van de gegeven string. Laat de lengte zijn N .
  4. Converteer de gegeven tekenreeks naar kleine letters om de vergelijking tussen de tekens niet hoofdlettergevoelig te maken.
  5. Initialiseer de lage indexvariabele als laag en zet deze op 0.
  6. Initialiseer de variabele met hoge index als hoog en stel deze in op n-1.
  7. Doe het volgende terwijl laag minder is dan hoog:
    • Vergelijk karakters bij lage index en hoge index.
    • Als de tekens niet overeenkomen, stelt u de vlag in op false en verbreekt u de lus.
    • Verhoog de waarde van laag met 1 en verlaag de waarde van hoog met 1.
  8. Als de vlag waar is aan het einde van de functie, betekent dit dat de gegeven string een palindroom is.
  9. Als de vlag aan het einde van de functie onwaar is, betekent dit dat de gegeven string geen palindroom is.

C++-programma om te controleren of een gegeven string een palindroom is of niet

Hieronder staat de C++-implementatie om te bepalen of de gegeven string een palindroom is of niet:

wat te doen als Facebook-account is gekloond
// 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;
}

Uitgang:



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

Python-programma om te controleren of een gegeven string een palindroom is of niet

Hieronder staat de Python-implementatie om te bepalen of de gegeven string een palindroom is of niet:

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

Uitgang:





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 Programma om te controleren of een gegeven string een palindroom is of niet

Hieronder staat de C-implementatie om te bepalen of de gegeven string een palindroom is of niet:

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

Uitgang:





muziek afspelen van telefoon naar auto
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

JavaScript-programma om te controleren of een gegeven string een palindroom is of niet

Hieronder staat de JavaScript-implementatie om te bepalen of de gegeven string een palindroom is of niet:

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

Uitgang:

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

Leer omgaan met strings in programmeren

Werken met strings is een integraal onderdeel van programmeren. Je moet weten hoe je strings moet gebruiken en manipuleren in een van de programmeertalen zoals Python, JavaScript, C++, etc.

Als u op zoek bent naar een taal om mee te beginnen, is Python een uitstekende keuze.

Deel Deel Tweeten E-mail Python leren? Hier leest u hoe u snaren kunt manipuleren

Het gebruiken en manipuleren van strings in Python kan moeilijk lijken, maar het is bedrieglijk eenvoudig.

Lees volgende
Gerelateerde onderwerpen
  • Programmeren
  • Codeerhandleidingen
Over de auteur Yuvraj Chandra(60 artikelen gepubliceerd)

Yuvraj is een student Computerwetenschappen aan de Universiteit van Delhi, India. Hij is gepassioneerd door Full Stack Web Development. Als hij niet aan het schrijven is, onderzoekt hij de diepte van verschillende technologieën.

Meer van Yuvraj Chandra

Abonneer op onze nieuwsbrief

Word lid van onze nieuwsbrief voor technische tips, recensies, gratis e-boeken en exclusieve deals!

Klik hier om je te abonneren