Πώς να ελέγξετε αν μια συμβολοσειρά είναι Palindrome

Πώς να ελέγξετε αν μια συμβολοσειρά είναι Palindrome

Μια συμβολοσειρά λέγεται ότι είναι παλίνδρομο εάν η αρχική συμβολοσειρά και το αντίστροφο της είναι ίδια. Σε αυτό το άρθρο, θα μάθετε για τον αλγόριθμο για να προσδιορίσετε εάν η δεδομένη συμβολοσειρά είναι παλίνδρομο ή όχι. Θα μάθετε επίσης πώς να εφαρμόσετε αυτόν τον αλγόριθμο στις πιο δημοφιλείς γλώσσες προγραμματισμού όπως C ++, Python, C και JavaScript.





Παραδείγματα χορδών Palindrome

Παρακάτω είναι μερικά παραδείγματα χορδών παλίνδρομου και μη παλίνδρομου:





Αλγόριθμος για τον προσδιορισμό αν μια δεδομένη συμβολοσειρά είναι παλίνδρομο ή όχι

Οι αλγόριθμοι είναι απλά μια σειρά οδηγιών που ακολουθούνται, βήμα προς βήμα, για να κάνετε κάτι χρήσιμο ή να λύσετε ένα πρόβλημα. Μπορείτε να λύσετε το πρόβλημα palindrome συμβολοσειράς χρησιμοποιώντας τον παρακάτω αλγόριθμο:





  1. Δηλώστε μια συνάρτηση που δέχεται τη δεδομένη συμβολοσειρά ως παράμετρο.
  2. Δημιουργήστε μια μεταβλητή boolean και ορίστε την σε true. Αφήστε τη μεταβλητή να είναι σημαία Το
  3. Βρείτε το μήκος της δεδομένης συμβολοσειράς. Αφήστε το μήκος να είναι ν Το
  4. Μετατρέψτε τη δεδομένη συμβολοσειρά σε πεζά για να κάνετε τη σύγκριση μεταξύ των χαρακτήρων χωρίς διάκριση πεζών-κεφαλαίων.
  5. Αρχικοποιήστε τη μεταβλητή χαμηλού δείκτη ως χαμηλός και ορίστε το στο 0.
  6. Αρχικοποιήστε τη μεταβλητή υψηλού δείκτη ως υψηλός και ορίστε το στο n-1.
  7. Κάντε τα εξής ενώ το χαμηλό είναι μικρότερο από το υψηλό:
    • Συγκρίνετε χαρακτήρες με χαμηλό δείκτη και υψηλό δείκτη.
    • Εάν οι χαρακτήρες δεν ταιριάζουν, ορίστε τη σημαία σε false και σπάστε το βρόχο.
    • Αυξήστε την τιμή του χαμηλού κατά 1 και μειώστε την τιμή του υψηλού κατά 1.
  8. Εάν η σημαία είναι αληθής στο τέλος της συνάρτησης, σημαίνει ότι η δεδομένη συμβολοσειρά είναι παλίνδρομο.
  9. Εάν η σημαία είναι ψευδής στο τέλος της συνάρτησης, σημαίνει ότι η δεδομένη συμβολοσειρά δεν είναι παλίνδρομο.

Πρόγραμμα C ++ για να ελέγξετε αν μια δεδομένη συμβολοσειρά είναι Palindrome ή όχι

Παρακάτω είναι η υλοποίηση C ++ για να καθοριστεί εάν η δεδομένη συμβολοσειρά είναι παλίνδρομη ή όχι:

πώς λειτουργεί η κάρτα wifi sd
// 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 για να καθοριστεί εάν η δεδομένη συμβολοσειρά είναι παλίνδρομο ή όχι:

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

Παραγωγή:





πώς να αποκτήσετε γρήγορα ένα σερί στο snapchat
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; Δείτε πώς μπορείτε να χειριστείτε τις χορδές

Η χρήση και ο χειρισμός συμβολοσειρών στην Python μπορεί να φαίνεται δύσκολη, αλλά είναι παραπλανητικά απλή.

Διαβάστε Επόμενο
Σχετικά θέματα
  • Προγραμματισμός
  • Φροντιστήρια κωδικοποίησης
Σχετικά με τον Συγγραφέα Γιουβράι Τσάντρα(Δημοσιεύθηκαν 60 άρθρα)

Ο Yuvraj είναι προπτυχιακός φοιτητής Πληροφορικής στο Πανεπιστήμιο του Δελχί, Ινδία. Είναι παθιασμένος με το Full Stack Web Development. Όταν δεν γράφει, εξερευνά το βάθος διαφορετικών τεχνολογιών.

Περισσότερα από τον Yuvraj Chandra

Εγγραφείτε στο newsletter μας

Εγγραφείτε στο ενημερωτικό μας δελτίο για τεχνικές συμβουλές, κριτικές, δωρεάν ebooks και αποκλειστικές προσφορές!

Κάντε κλικ εδώ για εγγραφή