Paano Suriin kung ang Isang String Ay Isang Palindrome

Paano Suriin kung ang Isang String Ay Isang Palindrome

Ang isang string ay sinasabing isang palindrome kung ang orihinal na string at ang reverse nito ay pareho. Sa artikulong ito, malalaman mo ang tungkol sa algorithm upang matukoy kung ang ibinigay na string ay isang palindrome o hindi. Malalaman mo rin kung paano ipatupad ang algorithm na ito sa pinakatanyag na mga wika sa pagprograma tulad ng C ++, Python, C, at JavaScript.





Mga halimbawa ng Palindrome String

Nasa ibaba ang ilang mga halimbawa ng mga palindrome at hindi palindrome na string:





Algorithm upang Tukuyin Kung ang Isang Naibigay na String Ay isang Palindrome o Hindi

Ang mga algorithm ay isang serye lamang ng mga tagubilin na sinusundan, sunud-sunod, upang makagawa ng isang bagay na kapaki-pakinabang o malutas ang isang problema. Maaari mong malutas ang problema sa palindrome ng string gamit ang algorithm sa ibaba:





  1. Ipahayag ang isang pagpapaandar na tumatanggap ng ibinigay na string bilang isang parameter.
  2. Lumikha ng isang variable ng boolean at itakda ito sa totoo. Hayaan ang variable ay bandila .
  3. Hanapin ang haba ng ibinigay na string. Hayaan ang haba maging n .
  4. I-convert ang ibinigay na string sa maliit na titik upang gawin ang paghahambing sa pagitan ng mga character na case-insensitive.
  5. Pasimulan ang mababang variable ng index bilang mababa at itakda ito sa 0.
  6. Simulan ang mataas na variable ng index bilang mataas at itakda ito sa n-1.
  7. Gawin ang sumusunod habang mababa ay mas mababa sa mataas:
    • Paghambingin ang mga character sa mababang index at mataas na index.
    • Kung hindi tumugma ang mga character, itakda ang flag sa false at basagin ang loop.
    • Palakihin ang halagang mababa ng 1 at bawasan ang halagang mataas ng 1.
  8. Kung ang watawat ay totoo sa pagtatapos ng pagpapaandar, nangangahulugan ito na ang ibinigay na string ay isang palindrome.
  9. Kung ang watawat ay mali sa pagtatapos ng pagpapaandar, nangangahulugan ito na ang ibinigay na string ay hindi isang palindrome.

C ++ Program upang Suriin Kung ang Isang Naibigay na String Ay isang Palindrome o Hindi

Nasa ibaba ang pagpapatupad ng C ++ upang matukoy kung ang ibinigay na string ay isang palindrome o hindi:

manuod ng mga bagong pelikula libre walang pag-sign up
// 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;
}

Output:



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 sa Python upang Suriin Kung ang Isang Naibigay na String Ay isang Palindrome o Hindi

Nasa ibaba ang pagpapatupad ng Python upang matukoy kung ang ibinigay na string ay isang palindrome o hindi:

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

Output:





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 upang Suriin Kung ang Isang Naibigay na String Ay isang Palindrome o Hindi

Nasa ibaba ang pagpapatupad ng C upang matukoy kung ang ibinigay na string ay isang palindrome o hindi:

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

Output:





ano ang volte sa aking telepono
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 sa JavaScript upang Suriin Kung ang Isang Naibigay na String Ay isang Palindrome o Hindi

Nasa ibaba ang pagpapatupad ng JavaScript upang matukoy kung ang ibinigay na string ay isang palindrome o hindi:

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

Output:

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

Alamin Kung Paano Makitungo Sa Mga String sa Programming

Ang pagtatrabaho sa mga string ay isang mahalagang bahagi ng programa. Dapat mong malaman kung paano gamitin at manipulahin ang mga string sa alinman sa mga wika sa pagprograma tulad ng Python, JavaScript, C ++, atbp.

Kung naghahanap ka para sa isang wika upang magsimula, ang Python ay isang mahusay na pagpipilian.

Magbahagi Magbahagi Mag-tweet Email Pag-aaral ng Python? Narito Kung Paano Manipulahin ang Mga String

Ang paggamit at pagmamanipula ng mga string sa Python ay maaaring magmukhang mahirap, ngunit ito ay mapanlinlang na deretso.

Basahin Susunod
Mga Kaugnay na Paksa
  • Programming
  • Mga Tutorial sa Coding
Tungkol sa May-akda Yuvraj Chandra(60 Mga Artikulo Na-publish)

Si Yuvraj ay isang undergraduate na mag-aaral sa Computer Science sa University of Delhi, India. Masigasig siya sa Full Stack Web Development. Kapag hindi siya nagsusulat, sinisiyasat niya ang lalim ng iba't ibang mga teknolohiya.

Higit pa Mula kay Yuvraj Chandra

Mag-subscribe sa aming newsletter

Sumali sa aming newsletter para sa mga tip sa tech, pagsusuri, libreng ebook, at eksklusibong deal!

Mag-click dito upang mag-subscribe