Cara Memeriksa Apakah String Adalah Palindrome

Cara Memeriksa Apakah String Adalah Palindrome

Rentetan dikatakan sebagai palindrome jika tali asal dan sebaliknya adalah sama. Dalam artikel ini, anda akan mempelajari algoritma untuk menentukan sama ada rentetan yang diberikan adalah palindrome atau tidak. Anda juga akan belajar bagaimana menerapkan algoritma ini dalam bahasa pengaturcaraan yang paling popular seperti C ++, Python, C, dan JavaScript.





Contoh Rentetan Palindrome

Berikut adalah beberapa contoh rentetan palindrome dan bukan palindrome:





Algoritma untuk Menentukan Sama ada Rentetan yang Diberi Adalah Palindrome atau Tidak

Algoritma hanyalah rangkaian arahan yang diikuti, langkah demi langkah, untuk melakukan sesuatu yang berguna atau menyelesaikan masalah. Anda boleh menyelesaikan masalah palindrome rentetan menggunakan algoritma di bawah:





  1. Menyatakan fungsi yang menerima rentetan yang diberikan sebagai parameter.
  2. Buat pemboleh ubah boolean dan tetapkan menjadi benar. Biarkan pemboleh ubah itu bendera .
  3. Cari panjang tali yang diberikan. Biarkan panjangnya n .
  4. Tukarkan rentetan yang diberikan menjadi huruf kecil untuk membuat perbandingan antara watak-huruf tanpa peka huruf besar.
  5. Memulakan pemboleh ubah indeks rendah sebagai rendah dan tetapkan ke 0.
  6. Memulakan pemboleh ubah indeks tinggi sebagai tinggi dan tetapkan ke n-1.
  7. Lakukan perkara berikut semasa rendah lebih rendah daripada tinggi:
    • Bandingkan watak pada indeks rendah dan indeks tinggi.
    • Sekiranya watak tidak sepadan, tetapkan bendera ke false dan putar gelung.
    • Menambah nilai rendah dengan 1 dan mengurangkan nilai tinggi dengan 1.
  8. Sekiranya bendera itu benar pada akhir fungsi, ini menandakan bahawa tali yang diberikan adalah palindrome.
  9. Sekiranya bendera salah pada akhir fungsi, ini menandakan bahawa tali yang diberikan bukan palindrome.

Program C ++ untuk Memeriksa Sama ada Rentetan yang Diberi Adalah Palindrome atau Tidak

Berikut adalah pelaksanaan C ++ untuk menentukan sama ada rentetan yang diberikan adalah palindrome atau tidak:

aplikasi untuk menghantar wang kepada rakan
// 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;
}

Pengeluaran:



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 Python untuk Memeriksa Sama ada Rentetan yang Diberikan Adalah Palindrome atau Tidak

Berikut adalah pelaksanaan Python untuk menentukan sama ada rentetan yang diberikan adalah palindrome atau tidak:

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

Pengeluaran:





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 C untuk Memeriksa Sama ada Rentetan yang Diberikan Adalah Palindrome atau Tidak

Di bawah ini adalah pelaksanaan C untuk menentukan apakah rentetan yang diberikan adalah palindrome atau tidak:

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

Pengeluaran:





bagaimana hotspot mudah alih berfungsi
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 JavaScript untuk Memeriksa Sama ada String yang Diberikan Adalah Palindrome atau Tidak

Berikut adalah pelaksanaan JavaScript untuk menentukan sama ada rentetan yang diberikan adalah palindrome atau tidak:

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

Pengeluaran:

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

Pelajari Cara Mengendalikan Rentetan dalam Pengaturcaraan

Bekerja dengan tali merupakan bahagian penting dalam pengaturcaraan. Anda mesti tahu bagaimana menggunakan dan memanipulasi rentetan dalam mana-mana bahasa pengaturcaraan seperti Python, JavaScript, C ++, dll.

Sekiranya anda mencari bahasa untuk memulakan, Python adalah pilihan yang sangat baik.

Berkongsi Berkongsi Tweet E-mel Belajar Python? Inilah Cara Memanipulasi Rentetan

Menggunakan dan memanipulasi tali di Python nampaknya sukar, tetapi secara langsung menipu.

Baca Seterusnya
Topik-topik yang berkaitan
  • Pengaturcaraan
  • Tutorial Pengekodan
Mengenai Pengarang Yuvraj Chandra(60 Artikel Diterbitkan)

Yuvraj adalah pelajar sarjana Sains Komputer di University of Delhi, India. Dia meminati Pembangunan Web Stack Penuh. Ketika dia tidak menulis, dia meneroka kedalaman teknologi yang berbeza.

Lagi Dari Yuvraj Chandra

Langgan buletin kami

Sertailah buletin kami untuk mendapatkan petua, ulasan, ebook percuma, dan tawaran eksklusif!

Klik di sini untuk melanggan