Hogyan lehet ellenőrizni, hogy a karakterlánc palindrom -e

Hogyan lehet ellenőrizni, hogy a karakterlánc palindrom -e

A karakterláncot akkor kell palindromnak tekinteni, ha az eredeti karakterlánc és a fordítottja megegyezik. Ebben a cikkben megismerheti az algoritmust annak meghatározására, hogy az adott karakterlánc palindrom -e vagy sem. Azt is megtanulhatja, hogyan kell megvalósítani ezt az algoritmust a legnépszerűbb programozási nyelveken, például a C ++, a Python, a C és a JavaScript.





Példák a Palindrome karakterláncra

Az alábbiakban néhány példa a palindrom és nem palindrom karakterláncokra:





Algoritmus annak meghatározására, hogy egy adott karakterlánc palindrom -e vagy sem

Az algoritmusok egyszerűen utasítások sorozata, amelyeket lépésről lépésre követnek, hogy valami hasznosat tegyenek vagy megoldjanak egy problémát. A string palindrome problémát az alábbi algoritmus segítségével oldhatja meg:



  1. Nyújtson be egy függvényt, amely elfogadja az adott karakterláncot paraméterként.
  2. Hozzon létre egy logikai változót, és állítsa true értékre. Legyen a változó zászló .
  3. Keresse meg az adott karakterlánc hosszát. Legyen a hossz n .
  4. A karakterek kisbetűs megkülönböztetéséhez alakítsa át az adott karakterláncot kisbetűssé.
  5. Inicializálja az alacsony indexű változót mint alacsony és állítsa 0 -ra.
  6. Inicializálja a magas indexű változót as magas és állítsa n-1-re.
  7. Tegye a következőket, amíg az alacsony érték alacsonyabb, mint a magas:
    • Hasonlítsa össze a karaktereket alacsony és magas indexekkel.
    • Ha a karakterek nem egyeznek, állítsa a zászlót hamisra, és szakítsa meg a hurkot.
    • Növelje az alacsony értéket 1 -gyel, és csökkentse a magas értéket 1 -gyel.
  8. Ha a jelző a függvény végén igaz, akkor azt jelzi, hogy az adott karakterlánc palindrom.
  9. Ha a zászló hamis a függvény végén, akkor azt jelzi, hogy az adott karakterlánc nem palindrom.

C ++ program annak ellenőrzésére, hogy egy adott karakterlánc palindrom -e vagy sem

Az alábbiakban bemutatjuk a C ++ megvalósítást annak megállapítására, hogy az adott karakterlánc palindrom -e vagy sem:

hogyan kell ss on snap -ot anélkül, hogy tudnák 2021
// 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;
}

Kimenet:



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 annak ellenőrzésére, hogy egy adott karakterlánc Palindrome -e vagy sem

Az alábbiakban a Python megvalósítása határozza meg, hogy az adott karakterlánc palindrom -e vagy sem:

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

Kimenet:





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 annak ellenőrzésére, hogy egy adott karakterlánc palindrom -e vagy sem

Az alábbiakban bemutatjuk a C implementációt annak megállapítására, hogy az adott karakterlánc palindrom -e vagy sem:

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

Kimenet:





hogyan jelenítse meg a telefon képernyőjét a számítógépen
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 annak ellenőrzésére, hogy egy adott karakterlánc Palindrome -e vagy sem

Az alábbiakban bemutatjuk a JavaScript megvalósítását annak megállapítására, hogy az adott karakterlánc palindrom -e vagy sem:

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

Kimenet:

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

Tanuld meg, hogyan kell bánni a karakterláncokkal a programozás során

A karakterláncokkal való munka a programozás szerves része. Tudnia kell, hogyan kell használni és kezelni a karakterláncokat bármely programozási nyelven, például Python, JavaScript, C ++ stb.

Ha olyan nyelvet keres, amellyel kiindulhat, a Python kiváló választás.

Részvény Részvény Csipog Email Python tanulása? Íme, hogyan lehet manipulálni a karakterláncokat

A karakterláncok használata és manipulálása a Pythonban nehéznek tűnhet, de megtévesztően egyszerű.

Olvassa tovább
Kapcsolódó témák
  • Programozás
  • Kódolási oktatóanyagok
A szerzőről Yuvraj Chandra(60 cikk megjelent)

Yuvraj egy számítástechnikai egyetemi hallgató a Delhi Egyetemen, Indiában. Szenvedélyesen foglalkozik a Full Stack webfejlesztéssel. Amikor nem ír, a különböző technológiák mélységét kutatja.

Bővebben: Yuvraj Chandra

Iratkozzon fel hírlevelünkre

Csatlakozz hírlevelünkhöz, ahol technikai tippeket, véleményeket, ingyenes e -könyveket és exkluzív ajánlatokat találsz!

Feliratkozáshoz kattintson ide