Kako preveriti, ali je niz palindrom

Kako preveriti, ali je niz palindrom

Niz naj bi bil palindrom, če sta izvirni niz in njegova hrbtna stran enaka. V tem članku boste izvedeli o algoritmu, da ugotovite, ali je dani niz palindrom ali ne. Naučili se boste tudi, kako ta algoritem implementirati v najbolj priljubljene programske jezike, kot so C ++, Python, C in JavaScript.





Primeri niza palindroma

Spodaj je nekaj primerov palindromskih in nepalindromskih strun:





Algoritem za ugotavljanje, ali je dani niz palindrom ali ne

Algoritmi so preprosto niz navodil, ki jih je treba korak za korakom narediti, da naredimo nekaj koristnega ali rešimo težavo. Problem palindroma niza lahko rešite s spodnjim algoritmom:





  1. Razglasite funkcijo, ki sprejme dani niz kot parameter.
  2. Ustvarite logično spremenljivko in jo nastavite na true. Naj bo spremenljivka zastavo .
  3. Poišči dolžino danega niza. Naj bo dolžina n .
  4. Pretvorite dani niz v male črke, da naredite primerjavo med znaki, ki ne razlikujejo med velikimi in malimi črkami.
  5. Inicializirajte spremenljivko nizkega indeksa kot nizka in ga nastavite na 0.
  6. Inicializirajte spremenljivko visokega indeksa kot visoko in ga nastavite na n-1.
  7. Naredite naslednje, medtem ko je nizko manj kot visoko:
    • Primerjajte znake z nizkim in visokim indeksom.
    • Če se znaki ne ujemajo, zastavico nastavite na false in prekinite zanko.
    • Povečajte vrednost nizkega za 1 in znižajte vrednost visokega za 1.
  8. Če je zastavica na koncu funkcije res, pomeni, da je dani niz palindrom.
  9. Če je zastavica na koncu funkcije napačna, to pomeni, da dani niz ni palindrom.

Program C ++ za preverjanje, ali je določen niz palindrom ali ne

Spodaj je izvedba C ++, da ugotovite, ali je dani niz palindrom ali ne:

aplikacije za pošiljanje denarja prijateljem
// 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;
}

Izhod:



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

Program Python za preverjanje, ali je dani niz palindrom ali ne

Spodaj je izvedba Pythona za določitev, ali je dani niz palindrom ali ne:

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

Izhod:





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 za preverjanje, ali je dani niz palindrom ali ne

Spodaj je izvedba C za določitev, ali je dani niz palindrom ali ne:

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

Izhod:





samsung galaxy watch 3 vs active 2
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

Program JavaScript za preverjanje, ali je dani niz palindrom ali ne

Spodaj je izvedba JavaScript, da ugotovite, ali je dani niz palindrom ali ne:

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

Izhod:

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

Naučite se ravnati z nizi v programiranju

Delo z nizi je sestavni del programiranja. Vedeti morate, kako uporabljati in upravljati nize v katerem koli programskem jeziku, kot so Python, JavaScript, C ++ itd.

Če iščete jezik za začetek, je Python odlična izbira.

Deliti Deliti Cvrkutati E-naslov Se učite Pythona? Tukaj je opisano, kako manipulirati z nizi

Uporaba in upravljanje nizov v Pythonu se lahko zdi težko, vendar je zavajajoče preprosto.

Preberite Naprej
Sorodne teme
  • Programiranje
  • Vadnice za kodiranje
O avtorju Yuvraj Chandra(Objavljenih 60 člankov)

Yuvraj je dodiplomski študent računalništva na Univerzi v Delhiju v Indiji. Navdušen je nad spletnim razvojem Full Stack. Ko ne piše, raziskuje globino različnih tehnologij.

Več od Yuvraja Chandre

Naročite se na naše novice

Pridružite se našemu glasilu za tehnične nasvete, ocene, brezplačne e -knjige in ekskluzivne ponudbe!

Kliknite tukaj, če se želite naročiti