Ako skontrolovať, či je reťazec palindróm

Ako skontrolovať, či je reťazec palindróm

Hovorí sa, že reťazec je palindróm, ak sú pôvodný reťazec a jeho rub rovnaké. V tomto článku sa dozviete o algoritme na určenie, či je daný reťazec palindróm alebo nie. Naučíte sa tiež implementovať tento algoritmus v najpopulárnejších programovacích jazykoch ako C ++, Python, C a JavaScript.





Príklady palindrómového reťazca

Nasleduje niekoľko príkladov palindrómových a nepalindrómových reťazcov:





Algoritmus na určenie, či je daný reťazec palindrómom alebo nie

Algoritmy sú jednoducho radom pokynov, ktoré sa postupujú krok za krokom, aby ste urobili niečo užitočné alebo vyriešili problém. Problém s palindrómom reťazca môžete vyriešiť pomocou nasledujúceho algoritmu:





  1. Deklarujte funkciu, ktorá akceptuje daný reťazec ako parameter.
  2. Vytvorte booleovskú premennú a nastavte ju na hodnotu true. Nech je premenná vlajka .
  3. Zistite dĺžku daného reťazca. Nech je dĺžka n .
  4. Konvertujte daný reťazec na malé písmená, aby porovnanie znakov nerozlišovalo veľké a malé písmena.
  5. Inicializujte premennú nízkeho indexu ako nízka a nastavte ho na 0.
  6. Inicializujte premennú vysokého indexu ako vysoká a nastavte ho na n-1.
  7. Postupujte takto, pokiaľ je nízka hodnota nižšia ako vysoká:
    • Porovnajte znaky s nízkym a vysokým indexom.
    • Ak sa znaky nezhodujú, nastavte príznak na hodnotu false a prerušte slučku.
    • Zvýšte hodnotu low o 1 a znížte hodnotu high o 1.
  8. Ak je príznak na konci funkcie pravdivý, znamená to, že daný reťazec je palindróm.
  9. Ak je príznak na konci funkcie nepravdivý, znamená to, že daný reťazec nie je palindróm.

Program C ++ na kontrolu, či je daný reťazec palindrómom alebo nie

Nasleduje implementácia C ++, ktorá určuje, či je daný reťazec palindrómom alebo nie:

čo je režim zmiznutia na Facebooku
// 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;
}

Výkon:



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 na kontrolu, či je daný reťazec palindrómom alebo nie

Nasleduje implementácia Pythonu, ktorá určuje, či je daný reťazec palindrómom alebo nie:

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

Výkon:





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 na kontrolu, či je daný reťazec palindrómom alebo nie

Nasleduje implementácia C, ktorá určuje, či je daný reťazec palindrómom alebo nie:

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

Výkon:





fungujú Apple airpods s Androidom
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 na kontrolu, či je daný reťazec palindrómom alebo nie

Nasleduje implementácia JavaScriptu na určenie, či je daný reťazec palindrómom alebo nie:

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

Výkon:

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

Zistite, ako sa vysporiadať so reťazcami v programovaní

Práca so reťazcami je neoddeliteľnou súčasťou programovania. Musíte vedieť, ako používať a manipulovať s reťazcami v ktoromkoľvek z programovacích jazykov, ako je Python, JavaScript, C ++ atď.

Ak hľadáte jazyk, s ktorým by ste mohli začať, Python je vynikajúcou voľbou.

zdieľam zdieľam Tweet E -mail Učíš sa Python? Tu je návod, ako manipulovať so strunami

Použitie a manipulácia so reťazcami v Pythone sa môže zdať ťažké, ale je to klamlivo jednoduché.

Čítajte ďalej
Súvisiace témy
  • Programovanie
  • Návody na kódovanie
O autorovi Yuvraj Chandra(60 článkov uverejnených)

Yuvraj je študentom informatiky na univerzite v Dillí v Indii. Je nadšený pre vývoj webových aplikácií Full Stack. Keď nepíše, skúma hĺbku rôznych technológií.

Viac od Yuvraja Chandru

prihlásiť sa ku odberu noviniek

Pripojte sa k nášmu bulletinu a získajte technické tipy, recenzie, bezplatné elektronické knihy a exkluzívne ponuky!

Kliknutím sem sa prihlásite na odber