Jak sprawdzić, czy struna jest palindromem?

Jak sprawdzić, czy struna jest palindromem?

Mówi się, że struna jest palindromem, jeśli oryginalny struna i jej rewers są takie same. W tym artykule poznasz algorytm określania, czy dany ciąg jest palindromem, czy nie. Dowiesz się również, jak zaimplementować ten algorytm w najpopularniejszych językach programowania, takich jak C++, Python, C i JavaScript.





Przykłady struny Palindrom

Poniżej znajduje się kilka przykładów ciągów palindromowych i niepalindromowych:





Algorytm do określenia, czy dany ciąg jest palindromem, czy nie

Algorytmy to po prostu seria instrukcji, które są wykonywane krok po kroku, aby zrobić coś pożytecznego lub rozwiązać problem. Możesz rozwiązać problem palindromu ciągów za pomocą poniższego algorytmu:





  1. Zadeklaruj funkcję, która akceptuje podany ciąg jako parametr.
  2. Utwórz zmienną logiczną i ustaw ją na true. Niech zmienna będzie flaga .
  3. Znajdź długość danego ciągu. Niech długość będzie n .
  4. Przekonwertuj podany ciąg na małe litery, aby porównanie między znakami nie było rozróżniane.
  5. Zainicjuj zmienną o niskim indeksie jako Niska i ustaw go na 0.
  6. Zainicjuj zmienną o wysokim indeksie jako wysoka i ustaw go na n-1.
  7. Wykonaj następujące czynności, gdy niski jest niższy niż wysoki:
    • Porównaj znaki o niskim i wysokim indeksie.
    • Jeśli znaki się nie zgadzają, ustaw flagę na false i przerwij pętlę.
    • Zwiększ wartość niskiego o 1 i zmniejsz wartość wysokiego o 1.
  8. Jeśli flaga na końcu funkcji jest prawdziwa, oznacza to, że dany ciąg jest palindromem.
  9. Jeśli flaga na końcu funkcji jest fałszywa, oznacza to, że dany ciąg nie jest palindromem.

Program C++ do sprawdzania, czy dany ciąg jest palindromem, czy nie

Poniżej znajduje się implementacja C++ w celu określenia, czy dany ciąg jest palindromem, czy nie:

jak zmienić domyślnego użytkownika w chrome?
// 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;
}

Wyjście:



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 Pythona do sprawdzania, czy dany ciąg jest palindromem, czy nie

Poniżej znajduje się implementacja Pythona określająca, czy dany ciąg jest palindromem, czy 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)

Wyjście:





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 C sprawdzający, czy dany ciąg jest palindromem, czy nie

Poniżej znajduje się implementacja C w celu określenia, czy dany ciąg jest palindromem, czy 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;
}

Wyjście:





czy możesz dodać barana do macbooka pro?
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 do sprawdzania, czy dany ciąg jest palindromem, czy nie

Poniżej znajduje się implementacja JavaScript w celu określenia, czy dany ciąg jest palindromem, czy 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);

Wyjście:

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

Dowiedz się, jak radzić sobie z ciągami znaków w programowaniu

Praca z ciągami znaków jest integralną częścią programowania. Musisz wiedzieć, jak używać i manipulować ciągami w dowolnym języku programowania, takim jak Python, JavaScript, C++ itp.

Jeśli szukasz języka na początek, Python jest doskonałym wyborem.

Udział Udział Ćwierkać E-mail Uczysz się Pythona? Oto jak manipulować strunami

Używanie i manipulowanie łańcuchami w Pythonie może wydawać się trudne, ale jest zwodniczo proste.

Czytaj dalej
Powiązane tematy
  • Programowanie
  • Poradniki kodowania
O autorze Yuvraj Chandra(60 opublikowanych artykułów)

Yuvraj jest studentem informatyki na Uniwersytecie w Delhi w Indiach. Jest pasjonatem Full Stack Web Development. Kiedy nie pisze, bada głębię różnych technologii.

Więcej od Yuvraja Chandra

Zapisz się do naszego newslettera

Dołącz do naszego newslettera, aby otrzymywać porady techniczne, recenzje, bezpłatne e-booki i ekskluzywne oferty!

Kliknij tutaj, aby zasubskrybować