Sådan kontrolleres, om en streng er et palindrom

Sådan kontrolleres, om en streng er et palindrom

En streng siges at være en palindrom, hvis den originale streng og dens bagside er den samme. I denne artikel lærer du om algoritmen til at afgøre, om den givne streng er en palindrom eller ej. Du lærer også, hvordan du implementerer denne algoritme på de mest populære programmeringssprog som C ++, Python, C og JavaScript.





Eksempler på Palindrome String

Nedenfor er nogle eksempler på palindrome og ikke-palindrome strenge:





Algoritme til at afgøre, om en given streng er en palindrom eller ej

Algoritmer er simpelthen en række instruktioner, der følges trin for trin for at gøre noget nyttigt eller løse et problem. Du kan løse problemet med strengens palindrom ved hjælp af nedenstående algoritme:





  1. Deklarere en funktion, der accepterer den givne streng som en parameter.
  2. Opret en boolsk variabel, og sæt den til sand. Lad variablen være flag .
  3. Find længden af ​​den givne streng. Lad længden være n .
  4. Konverter den givne streng til små bogstaver for at foretage sammenligningen mellem tegnene store og små bogstaver.
  5. Initialiser den lave indeksvariabel som lav og sæt den til 0.
  6. Initialiser højindeksvariablen som høj og indstil den til n-1.
  7. Gør følgende, mens lav er mindre end høj:
    • Sammenlign tegn ved lavt indeks og højt indeks.
    • Hvis tegnene ikke matchede, skal du indstille flaget til falsk og bryde løkken.
    • Forøg værdien af ​​lav med 1 og sænk værdien af ​​høj med 1.
  8. Hvis flaget er sandt i slutningen af ​​funktionen, betyder det, at den givne streng er en palindrom.
  9. Hvis flaget er falsk i slutningen af ​​funktionen, betyder det, at den givne streng ikke er en palindrom.

C ++ - program til at kontrollere, om en given streng er en Palindrome eller ej

Nedenfor er C ++ - implementeringen for at afgøre, om den givne streng er en palindrom eller ej:

har du brug for ps plus for at spille fortnite
// 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;
}

Produktion:



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 -program til at kontrollere, om en given streng er en Palindrome eller ej

Nedenfor er Python -implementeringen for at afgøre, om den givne streng er en palindrom eller ej:

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

Produktion:





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 -program til at kontrollere, om en given streng er en Palindrome eller ej

Nedenfor er C -implementeringen for at afgøre, om den givne streng er en palindrom eller ej:

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

Produktion:





hvordan man ved, om der tappes på mobiltelefonen
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 -program til at kontrollere, om en given streng er en Palindrome eller ej

Nedenfor er JavaScript -implementeringen for at afgøre, om den givne streng er en palindrom eller ej:

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

Produktion:

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

Lær, hvordan du håndterer strenge i programmering

Arbejde med strenge er en integreret del af programmeringen. Du skal vide, hvordan du bruger og manipulerer strenge i et hvilket som helst programmeringssprog som Python, JavaScript, C ++ osv.

Hvis du leder efter et sprog at starte med, er Python et glimrende valg.

Del Del Tweet E -mail Lærer du Python? Her er hvordan man manipulerer strenge

Brug og manipulation af strenge i Python kan virke svært, men det er vildledende ligetil.

Læs Næste
Relaterede emner
  • Programmering
  • Kodning Tutorials
Om forfatteren Yuvraj Chandra(60 artikler udgivet)

Yuvraj er en datalogi bachelorstuderende ved University of Delhi, Indien. Han brænder for Full Stack Web Development. Når han ikke skriver, undersøger han dybden af ​​forskellige teknologier.

Mere fra Yuvraj Chandra

Abonner på vores nyhedsbrev

Tilmeld dig vores nyhedsbrev for at få tekniske tips, anmeldelser, gratis e -bøger og eksklusive tilbud!

Klik her for at abonnere