Kā pārbaudīt, vai virkne ir palindroms

Kā pārbaudīt, vai virkne ir palindroms

Stīgu sauc par palindromu, ja sākotnējā virkne un tās reverss ir vienādi. Šajā rakstā jūs uzzināsit par algoritmu, lai noteiktu, vai norādītā virkne ir palindroms. Jūs arī uzzināsit, kā ieviest šo algoritmu populārākajās programmēšanas valodās, piemēram, C ++, Python, C un JavaScript.





Palindroma virknes piemēri

Zemāk ir daži palindromu un ne-palindromu virkņu piemēri:





Algoritms, lai noteiktu, vai dotā virkne ir palindroms vai nē

Algoritmi ir vienkārši instrukciju virkne, kas soli pa solim tiek ievērota, lai izdarītu kaut ko noderīgu vai atrisinātu problēmu. Stīgu palindroma problēmu var atrisināt, izmantojot šādu algoritmu:





  1. Deklarējiet funkciju, kas pieņem doto virkni kā parametru.
  2. Izveidojiet Būla mainīgo un iestatiet to uz patiesu. Ļaujiet mainīgajam būt karogs .
  3. Atrodiet dotās virknes garumu. Lai garums ir n .
  4. Pārveidojiet doto virkni par mazajiem burtiem, lai salīdzinātu rakstzīmes ar lielo un mazo burtu reģistru.
  5. Inicializējiet zema indeksa mainīgo kā zems un iestatiet to uz 0.
  6. Inicializējiet augsta indeksa mainīgo kā augsts un iestatiet to uz n-1.
  7. Veiciet šādas darbības, kamēr zemais ir mazāks par augstu:
    • Salīdziniet rakstzīmes ar zemu indeksu un augstu indeksu.
    • Ja rakstzīmes nesakrita, iestatiet karogu uz nepatiesu un pārtrauciet cilpu.
    • Palieliniet zemās vērtības vērtību par 1 un samaziniet augstās vērtības vērtību par 1.
  8. Ja karodziņš funkcijas beigās ir patiess, tas nozīmē, ka dotā virkne ir palindroms.
  9. Ja karodziņš funkcijas beigās ir nepatiess, tas nozīmē, ka norādītā virkne nav palindroms.

C ++ programma, lai pārbaudītu, vai dotā virkne ir palindroms

Zemāk ir C ++ ieviešana, lai noteiktu, vai norādītā virkne ir palindroms:

kas ir izzušanas režīms facebook messenger
// 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;
}

Izeja:



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 programma, lai pārbaudītu, vai dotā virkne ir palindroms

Zemāk ir Python ieviešana, lai noteiktu, vai dotā virkne ir palindroms:

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

Izeja:





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 programma, lai pārbaudītu, vai dotā virkne ir palindroms

Zemāk ir C ieviešana, lai noteiktu, vai norādītā virkne ir palindroms:

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

Izeja:





kā atiestatīt attēlu Mac datorā
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 programma, lai pārbaudītu, vai dotā virkne ir palindroms

Zemāk ir JavaScript ieviešana, lai noteiktu, vai norādītā virkne ir palindroms:

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

Izeja:

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

Uzziniet, kā tikt galā ar stīgām programmēšanā

Darbs ar virknēm ir neatņemama programmēšanas sastāvdaļa. Jums jāzina, kā izmantot un manipulēt ar virknēm jebkurā programmēšanas valodā, piemēram, Python, JavaScript, C ++ utt.

Ja jūs meklējat valodu, ar ko sākt, Python ir lieliska izvēle.

Kopīgot Kopīgot Čivināt E -pasts Mācīties Python? Lūk, kā manipulēt ar virknēm

Stīgu izmantošana un manipulēšana Python var šķist sarežģīta, taču maldinoši vienkārša.

Lasīt Tālāk
Saistītās tēmas
  • Programmēšana
  • Kodēšanas apmācības
Par autoru Yuvraj Chandra(Publicēti 60 raksti)

Yuvraj ir datorzinātņu bakalaura students Deli universitātē, Indijā. Viņš aizraujas ar Full Stack tīmekļa izstrādi. Kad viņš neraksta, viņš pēta dažādu tehnoloģiju dziļumu.

Vairāk no Yuvraj Chandra

Abonējiet mūsu biļetenu

Pievienojieties mūsu informatīvajam izdevumam, lai iegūtu tehniskus padomus, pārskatus, bezmaksas e -grāmatas un ekskluzīvus piedāvājumus!

Noklikšķiniet šeit, lai abonētu