In this C++ tutorial, you will learn how to check whether a given string is a palindrome. You will first use std::reverse(), then see a comparison-based approach that checks characters from both ends without creating a reversed copy.

What is a palindrome string in C++?

A string is a palindrome if it reads the same from left to right and from right to left. For example, madam, level, and moosoom are palindrome strings, while apple is not.

The simplest check is to reverse a copy of the input string and compare the reversed value with the original. If both strings are equal, the input is a palindrome.

C++ palindrome string program using reverse()

In the following program, we read a string into str, copy it to reversed, reverse the copy, and compare the two strings. The reverse() algorithm is declared in the <algorithm> header; a complete version with the standard headers is shown after this example.

main.cpp

</>
Copy
#include <iostream>
using namespace std;

int main() {
    string str, reversed;
    cout << "Enter a string : ";
    cin >> str;
    
    //make a copy of given string to reversed
    reversed = str;
    //and reverse the string
    reverse(reversed.begin(), reversed.end());
    
    //check if str equals reversed
    if (str == reversed) {
        cout << "The string is a Palindrome." << endl;
    } else {
        cout << "The string is not a Palindrome." << endl;
    }
}

Output

Enter a string : moosoom
The string is a Palindrome.
Program ended with exit code: 0
Enter a string : apple
The string is not a Palindrome.
Program ended with exit code: 0

How the reverse-and-compare palindrome check works

  1. Read the input into str.
  2. Copy str into reversed.
  3. Reverse the copied string.
  4. Compare str and reversed using ==.
  5. If they are equal, print that the string is a palindrome; otherwise, print that it is not.

reverse() is provided by the C++ standard library in the <algorithm> header. A portable program should also include <string> when using std::string. The following version shows the complete set of direct headers.

</>
Copy
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str, reversed;

    cout << "Enter a string : ";
    cin >> str;

    reversed = str;
    reverse(reversed.begin(), reversed.end());

    if (str == reversed) {
        cout << "The string is a Palindrome." << endl;
    } else {
        cout << "The string is not a Palindrome." << endl;
    }

    return 0;
}

C++ palindrome string program using a for loop

You do not have to reverse the entire string. Another common palindrome algorithm compares matching characters from the beginning and end of the string. The first character is compared with the last, the second with the second-last, and so on.

</>
Copy
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    bool isPalindrome = true;

    cout << "Enter a string : ";
    cin >> str;

    for (size_t i = 0; i < str.length() / 2; i++) {
        if (str[i] != str[str.length() - 1 - i]) {
            isPalindrome = false;
            break;
        }
    }

    if (isPalindrome) {
        cout << "The string is a Palindrome." << endl;
    } else {
        cout << "The string is not a Palindrome." << endl;
    }

    return 0;
}

Example output

Enter a string : level
The string is a Palindrome.

This approach stops as soon as it finds a mismatched pair. It uses constant extra space because it does not create a second string.

Palindrome comparison with spaces, punctuation, and letter case

The examples above compare characters exactly as entered. Therefore, uppercase and lowercase letters are different, and spaces or punctuation also affect the result. For example, Level is not equal to its exact reverse because L and l are different characters.

If your requirement is to treat a phrase such as Never odd or even as a palindrome, first define the comparison rules. A typical phrase-level check converts letters to one case and ignores spaces and punctuation before comparing characters. That is a different requirement from the exact string comparison used in the programs above.

Reading a palindrome phrase with getline()

The cin >> str input used above stops at whitespace, so it is suitable for a single word. To read an entire line containing spaces, use getline().

</>
Copy
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;

    cout << "Enter a string : ";
    getline(cin, str);

    cout << "You entered: " << str << endl;

    return 0;
}

This changes only how the text is read. If spaces and punctuation should be ignored during the palindrome check, normalize the text before comparing it.

Time and space complexity of the palindrome algorithms

Both methods inspect a number of characters proportional to the string length, so their time complexity is O(n).

  • Reverse and compare: uses O(n) extra space for the copied string.
  • Two-end comparison: uses O(1) extra space and may stop early when a mismatch is found.

Common mistakes in a C++ palindrome string program

  • Forgetting to include <algorithm> when calling reverse().
  • Using cin >> str when the input is expected to contain spaces.
  • Assuming the check is case-insensitive when the program performs exact character comparison.
  • Comparing characters beyond the middle of the string when a half-length loop is sufficient.
  • Using the wrong opposite index. For index i, the matching character from the end is at str.length() - 1 - i.

Editorial QA checklist for this C++ palindrome example

  • Confirm that examples labeled as palindromes read identically in reverse under the stated comparison rules.
  • Confirm that any example using reverse() includes or explains the <algorithm> header.
  • Check whether the input is intended to be one word or a full line, and use cin or getline() accordingly.
  • Verify that the two-end comparison uses str.length() - 1 - i and stops at the middle of the string.
  • State clearly whether case, spaces, and punctuation are significant to the palindrome test.

C++ palindrome string program summary

In this C++ Tutorial, we learned how to check if given string is a Palindrome or not.