कैसे जांचें कि कोई स्ट्रिंग एक पालिंड्रोम है या नहीं?

कैसे जांचें कि कोई स्ट्रिंग एक पालिंड्रोम है या नहीं?

एक स्ट्रिंग को पैलिंड्रोम कहा जाता है यदि मूल स्ट्रिंग और उसका उल्टा समान हो। इस लेख में, आप यह निर्धारित करने के लिए एल्गोरिदम के बारे में जानेंगे कि दी गई स्ट्रिंग एक पैलिंड्रोम है या नहीं। आप यह भी सीखेंगे कि इस एल्गोरिथम को C++, Python, C, और JavaScript जैसी सबसे लोकप्रिय प्रोग्रामिंग भाषाओं में कैसे लागू किया जाए।





पलिंड्रोम स्ट्रिंग के उदाहरण

नीचे पैलिंड्रोम और गैर-पैलिंड्रोम स्ट्रिंग्स के कुछ उदाहरण दिए गए हैं:





एल्गोरिदम यह निर्धारित करने के लिए कि क्या दिया गया स्ट्रिंग एक पालिंड्रोम है या नहीं

एल्गोरिथम केवल निर्देशों की एक श्रृंखला है, जिनका पालन चरण दर चरण, कुछ उपयोगी करने या किसी समस्या को हल करने के लिए किया जाता है। आप नीचे दिए गए एल्गोरिथम का उपयोग करके स्ट्रिंग पैलिंड्रोम समस्या को हल कर सकते हैं:





  1. एक फ़ंक्शन घोषित करें जो दिए गए स्ट्रिंग को पैरामीटर के रूप में स्वीकार करता है।
  2. एक बूलियन वैरिएबल बनाएं और इसे सही पर सेट करें। चर को होने दें झंडा .
  3. दिए गए तार की लंबाई ज्ञात कीजिए। लंबाई होने दें एन .
  4. केस-असंवेदनशील वर्णों के बीच तुलना करने के लिए दिए गए स्ट्रिंग को लोअरकेस में बदलें।
  5. निम्न सूचकांक चर को प्रारंभ करें कम और इसे 0 पर सेट करें।
  6. उच्च सूचकांक चर को प्रारंभ करें उच्च और इसे n-1 पर सेट करें।
  7. निम्न कार्य करें जबकि निम्न उच्च से कम है:
    • निम्न सूचकांक और उच्च सूचकांक पर वर्णों की तुलना करें।
    • यदि वर्ण मेल नहीं खाते हैं, तो ध्वज को गलत पर सेट करें और लूप को तोड़ दें।
    • निम्न के मान को 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

पायथन प्रोग्राम यह जाँचने के लिए कि दिया गया स्ट्रिंग एक पैलिंड्रोम है या नहीं

नीचे दी गई स्ट्रिंग एक पैलिंड्रोम है या नहीं यह निर्धारित करने के लिए पायथन कार्यान्वयन है:

# 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

सी प्रोग्राम यह जांचने के लिए कि कोई दिया गया स्ट्रिंग एक पालिंड्रोम है या नहीं

नीचे दी गई स्ट्रिंग एक पैलिंड्रोम है या नहीं यह निर्धारित करने के लिए सी कार्यान्वयन नीचे दिया गया है:

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

जावास्क्रिप्ट प्रोग्राम यह जाँचने के लिए कि दिया गया स्ट्रिंग एक पालिंड्रोम है या नहीं

नीचे दी गई स्ट्रिंग एक पैलिंड्रोम है या नहीं, यह निर्धारित करने के लिए जावास्क्रिप्ट कार्यान्वयन है:

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

प्रोग्रामिंग में स्ट्रिंग्स से निपटना सीखें

स्ट्रिंग्स के साथ काम करना प्रोग्रामिंग का एक अभिन्न अंग है। आपको पता होना चाहिए कि किसी भी प्रोग्रामिंग भाषा जैसे पायथन, जावास्क्रिप्ट, सी ++, आदि में स्ट्रिंग्स का उपयोग और हेरफेर कैसे करें।

यदि आप शुरू करने के लिए एक भाषा की तलाश कर रहे हैं, तो पायथन एक उत्कृष्ट विकल्प है।

साझा करना साझा करना कलरव ईमेल पायथन सीखना? यहां स्ट्रिंग्स में हेरफेर करने का तरीका बताया गया है

पायथन में स्ट्रिंग्स का उपयोग करना और उनमें हेरफेर करना मुश्किल लग सकता है, लेकिन यह भ्रामक रूप से सीधा है।

आगे पढ़िए
संबंधित विषय
  • प्रोग्रामिंग
  • कोडिंग ट्यूटोरियल
लेखक के बारे में युवराज चंद्र(60 लेख प्रकाशित)

युवराज दिल्ली विश्वविद्यालय, भारत में कंप्यूटर विज्ञान के स्नातक छात्र हैं। उन्हें फुल स्टैक वेब डेवलपमेंट का शौक है। जब वह नहीं लिख रहा होता है, तो वह विभिन्न तकनीकों की गहराई की खोज कर रहा होता है।

युवराज चंद्र की अन्य फ़िल्में-टीवी शो

हमारे न्यूज़लेटर की सदस्यता लें

तकनीकी युक्तियों, समीक्षाओं, निःशुल्क ई-पुस्तकों और अनन्य सौदों के लिए हमारे न्यूज़लेटर से जुड़ें!

सब्सक्राइब करने के लिए यहां क्लिक करें