يُقال أن السلسلة تكون متناظرة إذا كانت الوتر الأصلي وعكسه متماثلان. في هذه المقالة ، ستتعرف على الخوارزمية لتحديد ما إذا كانت السلسلة المحددة متماثلة أم لا. ستتعلم أيضًا كيفية تنفيذ هذه الخوارزمية في لغات البرمجة الأكثر شيوعًا مثل C ++ و Python و C و JavaScript.
أمثلة على سلسلة Palindrome
فيما يلي بعض الأمثلة على السلاسل المتناظرة وغير المتناظرة:
خوارزمية لتحديد ما إذا كانت السلسلة المعطاة متناظرة أم لا
الخوارزميات هي ببساطة سلسلة من التعليمات التي يتم اتباعها خطوة بخطوة للقيام بشيء مفيد أو لحل مشكلة ما. يمكنك حل مشكلة تناظر السلسلة باستخدام الخوارزمية التالية:
- قم بتعريف دالة تقبل السلسلة المحددة كمعامل.
- أنشئ متغيرًا منطقيًا واضبطه على صحيح. دع المتغير يكون علم .
- أوجد طول السلسلة المحددة. دع الطول يكون ن .
- قم بتحويل السلسلة المحددة إلى أحرف صغيرة لإجراء المقارنة بين الأحرف غير حساسة لحالة الأحرف.
- تهيئة متغير المؤشر المنخفض كـ قليل وضبطها على 0.
- تهيئة متغير المؤشر المرتفع كـ عالي وضبطه على n-1.
- قم بما يلي بينما تكون القيمة المنخفضة أقل من عالية:
- قارن بين الأحرف ذات الفهرس المنخفض والفهرس العالي.
- إذا لم تتطابق الأحرف ، فاضبط العلم على خطأ واقطع الحلقة.
- قم بزيادة قيمة منخفض بمقدار 1 وتقليل قيمة مرتفع بمقدار 1.
- إذا كان العلم صحيحًا في نهاية الوظيفة ، فهذا يدل على أن السلسلة المحددة هي متناظرة.
- إذا كانت العلامة خاطئة في نهاية الوظيفة ، فهذا يدل على أن السلسلة المحددة ليست متناظرة.
برنامج C ++ للتحقق مما إذا كانت السلسلة المعينة متناظرة أم لا
يوجد أدناه تطبيق C ++ لتحديد ما إذا كانت السلسلة المحددة متناظرة أم لا:
كيف تلعب google slides on loop
// 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 للتحقق مما إذا كانت السلسلة المعطاة متناظرة أم لا
يوجد أدناه تطبيق Python لتحديد ما إذا كانت السلسلة المحددة متناظرة أم لا:
# 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 للتحقق مما إذا كانت السلسلة المعطاة متطابقة أم لا
يوجد أدناه تطبيق 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;
}
انتاج:
كيفية التحقق من قدرة بطارية android
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 خيار ممتاز.
يشارك يشارك سقسقة بريد الالكتروني تعلم بايثون؟ إليك كيفية التعامل مع السلاسلقد يبدو استخدام الجمل في بايثون ومعالجتها أمرًا صعبًا ، لكنه واضح ومخادع.
اقرأ التالي مواضيع ذات صلة- برمجة
- دروس الترميز
يوفراج طالب جامعي في علوم الكمبيوتر بجامعة دلهي بالهند. إنه شغوف بتطوير الويب Full Stack. عندما لا يكتب ، فإنه يستكشف عمق التقنيات المختلفة.
المزيد من Yuvraj Chandraاشترك في نشرتنا الإخبارية
انضم إلى النشرة الإخبارية لدينا للحصول على نصائح تقنية ومراجعات وكتب إلكترونية مجانية وصفقات حصرية!
انقر هنا للاشتراك