วิธีตรวจสอบว่าสตริงเป็น Palindrome หรือไม่

วิธีตรวจสอบว่าสตริงเป็น Palindrome หรือไม่

กล่าวกันว่าสตริงเป็นพาลินโดรมหากสตริงเดิมและด้านหลังเหมือนกัน ในบทความนี้ คุณจะได้เรียนรู้เกี่ยวกับอัลกอริทึมเพื่อพิจารณาว่าสตริงที่ระบุนั้นเป็นพาลินโดรมหรือไม่ คุณจะได้เรียนรู้วิธีใช้อัลกอริทึมนี้ในภาษาโปรแกรมยอดนิยม เช่น C++, Python, C และ JavaScript





ตัวอย่างของ Palindrome String

ด้านล่างนี้เป็นตัวอย่างบางส่วนของสตริง palindrome และ non-palindrome:





อัลกอริทึมในการพิจารณาว่าสตริงที่ระบุเป็น Palindrome หรือไม่

อัลกอริธึมเป็นเพียงชุดคำสั่งที่ตามมา ทีละขั้นตอน เพื่อทำสิ่งที่มีประโยชน์หรือแก้ปัญหา คุณสามารถแก้ปัญหาสตริง palindrome โดยใช้อัลกอริทึมด้านล่าง:





  1. ประกาศฟังก์ชันที่ยอมรับสตริงที่กำหนดเป็นพารามิเตอร์
  2. สร้างตัวแปรบูลีนและตั้งค่าเป็นจริง ให้ตัวแปร be ธง .
  3. ค้นหาความยาวของสตริงที่กำหนด ให้ความยาวเป็น NS .
  4. แปลงสตริงที่กำหนดเป็นตัวพิมพ์เล็กเพื่อทำการเปรียบเทียบระหว่างอักขระที่ไม่คำนึงถึงขนาดตัวพิมพ์
  5. เริ่มต้นตัวแปรดัชนีต่ำเป็น ต่ำ และตั้งค่าเป็น 0
  6. เริ่มต้นตัวแปรดัชนีสูงเป็น สูง และตั้งค่าเป็น n-1
  7. ทำสิ่งต่อไปนี้ในขณะที่ต่ำน้อยกว่าสูง:
    • เปรียบเทียบอักขระที่ดัชนีต่ำและดัชนีสูง
    • หากอักขระไม่ตรงกัน ให้ตั้งค่าสถานะเป็นเท็จและทำลายลูป
    • เพิ่มค่าต่ำ 1 และลดค่าสูง 1
  8. ถ้าแฟล็กเป็นจริงเมื่อสิ้นสุดฟังก์ชัน แสดงว่าสตริงที่กำหนดคือ palindrome
  9. ถ้าแฟล็กเป็นเท็จที่ส่วนท้ายของฟังก์ชัน แสดงว่าสตริงที่กำหนดไม่ใช่ palindrome

โปรแกรม C ++ เพื่อตรวจสอบว่าสตริงที่ระบุเป็น Palindrome หรือไม่

ด้านล่างนี้คือการใช้งาน C++ เพื่อตรวจสอบว่าสตริงที่กำหนดนั้นเป็นพาลินโดรมหรือไม่:

หาหนังสือตามคำอธิบายของโครงเรื่อง
// 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;
}

เอาท์พุท:



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 เพื่อตรวจสอบว่าสตริงที่ระบุเป็น Palindrome หรือไม่

ด้านล่างนี้คือการนำ Python ไปใช้งานเพื่อตรวจสอบว่าสตริงที่กำหนดเป็น palindrome หรือไม่:

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

เอาท์พุท:





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 เพื่อตรวจสอบว่าสตริงที่ระบุเป็น Palindrome หรือไม่

ด้านล่างนี้คือการใช้งาน C เพื่อตรวจสอบว่าสตริงที่กำหนดเป็นพาลินโดรมหรือไม่:

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

เอาท์พุท:





โซน ar ในโทรศัพท์ของฉันคืออะไร
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 เพื่อตรวจสอบว่าสตริงที่ระบุเป็น Palindrome หรือไม่

ด้านล่างนี้คือการใช้งาน JavaScript เพื่อตรวจสอบว่าสตริงที่กำหนดเป็นพาลินโดรมหรือไม่:

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

เอาท์พุท:

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, JavaScript, C++ เป็นต้น

หากคุณกำลังมองหาภาษาเพื่อเริ่มต้นใช้งาน Python เป็นตัวเลือกที่ยอดเยี่ยม

แบ่งปัน แบ่งปัน ทวีต อีเมล การเรียนรู้หลาม? นี่คือวิธีจัดการกับสตริง

การใช้และจัดการสตริงใน Python อาจดูยาก แต่ก็ตรงไปตรงมา

อ่านต่อไป
หัวข้อที่เกี่ยวข้อง
  • การเขียนโปรแกรม
  • บทเรียนการเข้ารหัส
เกี่ยวกับผู้เขียน ยุวราช จันทรา(60 บทความที่ตีพิมพ์)

Yuvraj เป็นนักศึกษาระดับปริญญาตรีสาขาวิทยาการคอมพิวเตอร์ที่มหาวิทยาลัยเดลี ประเทศอินเดีย เขาหลงใหลเกี่ยวกับ Full Stack Web Development เมื่อไม่ได้เขียน เขากำลังสำรวจความลึกของเทคโนโลยีต่างๆ

เพิ่มเติมจาก Yuvraj Chandra

สมัครรับจดหมายข่าวของเรา

เข้าร่วมจดหมายข่าวของเราสำหรับเคล็ดลับทางเทคนิค บทวิจารณ์ eBook ฟรี และดีลพิเศษ!

คลิกที่นี่เพื่อสมัครสมาชิก