Kaip patikrinti, ar eilutė yra palindromas

Kaip patikrinti, ar eilutė yra palindromas

Sakoma, kad eilutė yra palindromas, jei originali eilutė ir jos reversas yra vienodi. Šiame straipsnyje sužinosite apie algoritmą, pagal kurį nustatoma, ar nurodyta eilutė yra palindromas, ar ne. Taip pat sužinosite, kaip įgyvendinti šį algoritmą populiariausiose programavimo kalbose, tokiose kaip C ++, Python, C ir JavaScript.





Palindromo eilutės pavyzdžiai

Žemiau pateikiami keli palindromo ir ne palindromo stygų pavyzdžiai:





Algoritmas, skirtas nustatyti, ar tam tikra eilutė yra palindromas, ar ne

Algoritmai yra tik instrukcijų serija, kuri žingsnis po žingsnio vykdoma norint padaryti kažką naudingo ar išspręsti problemą. Eilutės palindromo problemą galite išspręsti naudodami šį algoritmą:





  1. Paskelbkite funkciją, kuri priima nurodytą eilutę kaip parametrą.
  2. Sukurkite loginį kintamąjį ir nustatykite jį į true. Tegul kintamasis būna vėliava .
  3. Raskite nurodytos eilutės ilgį. Tegul ilgis būna n .
  4. Konvertuokite nurodytą eilutę į mažąsias, kad būtų galima palyginti didžiųjų ir mažųjų raidžių reikšmes.
  5. Inicijuokite žemo indekso kintamąjį kaip žemas ir nustatykite jį į 0.
  6. Inicijuokite aukšto indekso kintamąjį kaip aukštas ir nustatykite jį į n-1.
  7. Jei žemas lygis yra mažesnis nei aukštas, atlikite šiuos veiksmus:
    • Palyginkite mažo ir aukšto indekso simbolius.
    • Jei simboliai nesutampa, nustatykite vėliavą kaip klaidingą ir nutraukite kilpą.
    • Padidinkite žemos reikšmę 1 ir sumažinkite aukšto reikšmę 1.
  8. Jei funkcijos pabaigoje vėliava yra teisinga, tai reiškia, kad nurodyta eilutė yra palindromas.
  9. Jei funkcijos pabaigoje vėliava yra klaidinga, tai reiškia, kad nurodyta eilutė nėra palindromas.

C ++ programa, skirta patikrinti, ar tam tikra eilutė yra palindromas, ar ne

Žemiau yra C ++ diegimas, skirtas nustatyti, ar pateikta eilutė yra palindromas, ar ne:

klausytis muzikos internete nemokamai ir neatsisiųsti
// 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;
}

Išėjimas:



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“ programa, skirta patikrinti, ar tam tikra eilutė yra palindromas, ar ne

Žemiau yra „Python“ diegimas, skirtas nustatyti, ar nurodyta eilutė yra palindromas, ar 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)

Išėjimas:





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 programa, skirta patikrinti, ar tam tikra eilutė yra palindromas, ar ne

Žemiau yra C diegimas, siekiant nustatyti, ar pateikta eilutė yra palindromas, ar 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;
}

Išėjimas:





iš naujo nustatyti vietinio administratoriaus slaptažodį „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

„JavaScript“ programa, skirta patikrinti, ar tam tikra eilutė yra palindromas, ar ne

Žemiau pateikiamas „JavaScript“ diegimas, siekiant nustatyti, ar pateikta eilutė yra palindromas, ar 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);

Išėjimas:

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

Sužinokite, kaip susidoroti su programavimo eilėmis

Darbas su eilutėmis yra neatskiriama programavimo dalis. Jūs turite žinoti, kaip naudoti ir manipuliuoti eilutėmis bet kuria programavimo kalba, pvz., „Python“, „JavaScript“, C ++ ir kt.

Jei ieškote kalbos, nuo kurios pradėti, „Python“ yra puikus pasirinkimas.

Dalintis Dalintis „Tweet“ Paštu Mokytis Python? Štai kaip manipuliuoti eilutėmis

Naudoti ir manipuliuoti eilutėmis „Python“ gali atrodyti sunku, tačiau tai apgaulingai paprasta.

Skaityti toliau
Susijusios temos
  • Programavimas
  • Kodavimo pamokos
Apie autorių Yuvraj Chandra(Paskelbti 60 straipsnių)

Yuvraj yra kompiuterių mokslo bakalauro studentas Delyje, Indijoje. Jis aistringas „Full Stack“ žiniatinklio kūrimui. Kai jis nerašo, jis tyrinėja skirtingų technologijų gylį.

Daugiau iš Yuvraj Chandra

Prenumeruokite mūsų naujienlaiškį

Prisijunkite prie mūsų naujienlaiškio, kad gautumėte techninių patarimų, apžvalgų, nemokamų el. Knygų ir išskirtinių pasiūlymų!

Norėdami užsiprenumeruoti, spustelėkite čia