Jak zkontrolovat, zda je řetězec palindrom

Jak zkontrolovat, zda je řetězec palindrom

Řetězec je považován za palindrom, pokud je původní řetězec a jeho rub stejné. V tomto článku se dozvíte o algoritmu k určení, zda je daný řetězec palindrom nebo ne. Naučíte se také, jak implementovat tento algoritmus v nejpopulárnějších programovacích jazycích, jako je C ++, Python, C a JavaScript.





Příklady palindromského řetězce

Níže jsou uvedeny některé příklady palindromových a nepalindromových řetězců:





Algoritmus k určení, zda je daný řetězec palindromem nebo ne

Algoritmy jsou prostě série pokynů, které se krok za krokem dodržují, aby udělaly něco užitečného nebo vyřešily problém. Problém palindromu řetězců můžete vyřešit pomocí níže uvedeného algoritmu:





  1. Deklarujte funkci, která přijímá daný řetězec jako parametr.
  2. Vytvořte booleovskou proměnnou a nastavte ji na true. Nechte proměnnou být vlajka .
  3. Najděte délku daného řetězce. Nechte délku být n .
  4. Převeďte daný řetězec na malá písmena, aby srovnání mezi znaky nerozlišovalo velká a malá písmena.
  5. Inicializujte proměnnou s nízkým indexem jako nízký a nastavit na 0.
  6. Inicializujte proměnnou vysokého indexu jako vysoký a nastavte jej na n-1.
  7. Proveďte následující kroky, zatímco nízká je menší než vysoká:
    • Porovnejte znaky s nízkým a vysokým indexem.
    • Pokud se znaky neshodují, nastavte příznak na hodnotu false a přerušte smyčku.
    • Zvyšte hodnotu low o 1 a snižte hodnotu high o 1.
  8. Pokud je příznak na konci funkce pravdivý, znamená to, že daný řetězec je palindrom.
  9. Pokud je příznak na konci funkce nepravdivý, znamená to, že daný řetězec není palindrom.

Program C ++ ke kontrole, zda je daný řetězec palindromem nebo ne

Níže je implementace C ++ k určení, zda je daný řetězec palindrom nebo ne:

jak přejít zpět na klasický gmail
// 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ýstup:



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 ke kontrole, zda je daný řetězec palindromem nebo ne

Níže je implementace Pythonu, která určuje, zda je daný řetězec palindrom nebo 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)

Výstup:





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 ke kontrole, zda je daný řetězec palindromem nebo ne

Níže je implementace C, která určuje, zda je daný řetězec palindrom nebo 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;
}

Výstup:





otevřít soubory .jar Windows 10
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 ke kontrole, zda je daný řetězec palindromem nebo ne

Níže je implementace JavaScriptu, která určuje, zda je daný řetězec palindrom nebo 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);

Výstup:

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

Zjistěte, jak zacházet s řetězci v programování

Práce s řetězci je nedílnou součástí programování. Musíte vědět, jak používat a manipulovat s řetězci v kterémkoli z programovacích jazyků, jako je Python, JavaScript, C ++ atd.

Pokud hledáte jazyk, se kterým byste mohli začít, Python je skvělou volbou.

Podíl Podíl tweet E-mailem Učit se Python? Zde je návod, jak manipulovat s řetězci

Používání a manipulace s řetězci v Pythonu se může zdát obtížné, ale je klamně jednoduché.

Číst dále
Související témata
  • Programování
  • Návody na kódování
O autorovi Yuvraj Chandra(60 článků zveřejněno)

Yuvraj je studentem informatiky na univerzitě v Dillí v Indii. Je nadšený pro webový vývoj Full Stack. Když nepíše, zkoumá hloubku různých technologií.

Více od Yuvraj Chandra

Přihlaste se k odběru našeho zpravodaje

Připojte se k našemu zpravodaji a získejte technické tipy, recenze, bezplatné elektronické knihy a exkluzivní nabídky!

Kliknutím sem se přihlásíte k odběru