Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIC++ Program to check if a given String is Palindrome or not

C++ Program to check if a given String is Palindrome or not

Given a string S consisting of N characters of the English alphabet, the task is to check if the given string is a palindrome. If the given string is a palindrome, then print “Yes“. Otherwise, print “No“.

Note:  A string is said to be palindrome if the reverse of the string is the same as the string. 

For example, The string “racecar” is a palindrome because the reverse of the string is the same as the original string.

palindrome string in c++

 

Method 1: Using the Inbuilt reverse() Function in the STL

The simplest approach is to use the inbuilt reverse() function in the STL.

Algorithm

  • Copy the string S to another string, say P, and then reverse the string S.
  • Now check if the string S is equal to the string P and then print “Yes“. Otherwise, print “No“.

C++ Program to Check if a Given String is Palindrome or Not

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether
// the string is palindrome
string isPalindrome(string S)
{
    // Stores the reverse of the
    // string S
    string P = S;
 
    // Reverse the string P
    reverse(P.begin(), P.end());
 
    // If S is equal to P
    if (S == P) {
        // Return "Yes"
        return "Yes";
    }
    // Otherwise
    else {
        // return "No"
        return "No";
    }
}
 
// Driver Code
int main()
{
    string S = "ABCDCBA";
    cout << isPalindrome(S);
 
    return 0;
}


Output

Yes

Complexity Analysis

  • Time Complexity: O(N)
  • Auxiliary Space: O(N)

Method 2: By Traversing the String

The above approach can be optimized in space complexity by traversing the string and checking whether the character at the ith index is equal to the character at the (N-i-1)th index for every index in the range [0, N/2].

Algorithm

  • Iterate over the range [0, N/2], using the variable i, and in each iteration check if the character at index i and N-i-1 are not equal, then print “No” and break.
  • If none of the above cases satisfy, then print “Yes“.

C++ Program to Check if a Given String is Palindrome or Not

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether string
// is palindrome
string isPalindrome(string S)
{
    // Iterate over the range [0, N/2]
    for (int i = 0; i < S.length() / 2; i++) {
 
        // If S[i] is not equal to
        // the S[N-i-1]
        if (S[i] != S[S.length() - i - 1]) {
            // Return No
            return "No";
        }
    }
    // Return "Yes"
    return "Yes";
}
 
// Driver Code
int main()
{
    string S = "ABCDCBA";
    cout << isPalindrome(S);
 
    return 0;
}


Output

Yes

Complexity Analysis

  • Time Complexity: O(N)
  • Auxiliary Space: O(1)

Related Articles

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments