כיצד לבדוק אם מחרוזת היא פלינדרום

כיצד לבדוק אם מחרוזת היא פלינדרום

אומרים כי מחרוזת היא פלינדרום אם המחרוזת המקורית וההפוכה שלה זהים. במאמר זה תלמד על האלגוריתם כדי לקבוע אם המחרוזת הנתונה היא פלינדרום או לא. כמו כן, תלמד כיצד ליישם אלגוריתם זה בשפות התכנות הפופולריות ביותר כמו C ++, Python, C ו- JavaScript.





דוגמאות למחרוזת פלינדרום

להלן מספר דוגמאות למחרוזות של פלינדרום ולא-פלינדרום:





אלגוריתם לקביעה אם מחרוזת נתונה היא פלינדרום או לא

אלגוריתמים הם פשוט סדרה של הוראות שעוקבות אחריהם, צעד אחר צעד, כדי לעשות משהו מועיל או לפתור בעיה. אתה יכול לפתור את בעיית המחרוזת palindrome באמצעות האלגוריתם שלהלן:





  1. הכריז על פונקציה המקבלת את המחרוזת הנתונה כפרמטר.
  2. צור משתנה בוליאני והגדר אותו כ- true. תן למשתנה להיות דֶגֶל .
  3. מצא את אורך המחרוזת הנתונה. תנו לאורך להיות נ .
  4. המר את המחרוזת הנתונה לאותיות קטנות כדי לבצע את ההשוואה בין התווים ללא רגישות.
  5. אתחל את משתנה המדד הנמוך כ נָמוּך ותגדיר אותו ל 0.
  6. אתחל את משתנה המדד הגבוה כ גָבוֹהַ והגדר אותו ל- n-1.
  7. בצע את הפעולות הבאות כאשר הנמוך נמוך מהגבוה:
    • השווה תווים באינדקס נמוך ואינדקס גבוה.
    • אם התווים לא התאימו, הגדר את הדגל כ- false ושבור את הלולאה.
    • העלו את ערך הנמוך ב -1 והורידו את הערך של הגבוה ב -1.
  8. אם הדגל נכון בסוף הפונקציה, הוא מסמל שהמחרוזת הנתונה היא פלינדרום.
  9. אם הדגל שגוי בסוף הפונקציה, הוא מסמל שהמחרוזת הנתונה אינה פלינדרום.

תוכנית C ++ כדי לבדוק אם מחרוזת נתונה היא פלינדרום או לא

להלן יישום 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 לבדוק אם מחרוזת נתונה היא פלינדרום או לא

להלן יישום 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;
}

תְפוּקָה:





כיצד למחוק גיבויים ממכונת הזמן
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 לבדוק האם מחרוזת נתונה היא פלינדרום או לא

להלן יישום 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 ++ וכו '.

אם אתם מחפשים שפה להתחיל איתה, פייתון היא בחירה מצוינת.

לַחֲלוֹק לַחֲלוֹק צִיוּץ אימייל לומדים פייתון? להלן אופן מניפולציה של מחרוזות

שימוש ומניפולציה של מחרוזות בפייתון עשוי להיראות קשה, אך הוא פשוט באופן מטעה.

קרא הבא
נושאים קשורים
  • תִכנוּת
  • הדרכות קידוד
על הסופר יובראג 'צ'נדרה(פורסמו 60 מאמרים)

יובראג 'הוא סטודנט לתואר ראשון במדעי המחשב באוניברסיטת דלהי, הודו. הוא נלהב מ- Full Stack Web Development. כשהוא לא כותב, הוא בוחן את עומק הטכנולוגיות השונות.

עוד מאת Yuvraj Chandra

הירשם לניוזלטר שלנו

הצטרף לניוזלטר שלנו לקבלת טיפים, סקירות, ספרים אלקטרוניים בחינם ומבצעים בלעדיים!

לחצו כאן להרשמה