0
0
CppProgramBeginner · 2 min read

C++ Program to Check Palindrome String

A C++ program to check a palindrome string compares characters from the start and end using a loop with for or while and returns true if all pairs match; for example, bool isPalindrome(string s) { int n = s.size(); for (int i = 0; i < n / 2; i++) if (s[i] != s[n - i - 1]) return false; return true; }.
📋

Examples

Inputmadam
Outputmadam is a palindrome
Inputhello
Outputhello is not a palindrome
Inputa
Outputa is a palindrome
🧠

How to Think About It

To check if a string is a palindrome, think of reading it from both ends at the same time. Compare the first character with the last, the second with the second last, and so on. If all pairs match, the string reads the same forwards and backwards, so it is a palindrome.
📐

Algorithm

1
Get the input string.
2
Find the length of the string.
3
Compare characters from the start and end moving towards the center.
4
If any pair does not match, conclude the string is not a palindrome.
5
If all pairs match, conclude the string is a palindrome.
6
Print the result.
💻

Code

cpp
#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(string s) {
    int n = s.size();
    for (int i = 0; i < n / 2; i++) {
        if (s[i] != s[n - i - 1])
            return false;
    }
    return true;
}

int main() {
    string str;
    cout << "Enter a string: ";
    cin >> str;
    if (isPalindrome(str))
        cout << str << " is a palindrome" << endl;
    else
        cout << str << " is not a palindrome" << endl;
    return 0;
}
Output
Enter a string: madam madam is a palindrome
🔍

Dry Run

Let's trace the string 'madam' through the code

1

Input string

str = 'madam'

2

Calculate length

n = 5

3

Compare characters

Compare s[0] = 'm' and s[4] = 'm' -> match Compare s[1] = 'a' and s[3] = 'a' -> match Loop ends as i < n/2 (i < 2)

4

Result

All pairs matched, return true

is[i]s[n-i-1]Match?
0mmYes
1aaYes
💡

Why This Works

Step 1: Check pairs from ends

The code compares characters from the start and end moving inward using for loop.

Step 2: Return false on mismatch

If any pair of characters differ, the function returns false immediately.

Step 3: Return true if all match

If the loop finishes without mismatches, the string is a palindrome, so it returns true.

🔄

Alternative Approaches

Using two pointers
cpp
#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(string s) {
    int left = 0, right = s.size() - 1;
    while (left < right) {
        if (s[left] != s[right])
            return false;
        left++;
        right--;
    }
    return true;
}

int main() {
    string str;
    cout << "Enter a string: ";
    cin >> str;
    cout << str << (isPalindrome(str) ? " is a palindrome" : " is not a palindrome") << endl;
    return 0;
}
This uses two pointers moving towards the center, which is easy to understand and efficient.
Using string reverse
cpp
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    string str, rev;
    cout << "Enter a string: ";
    cin >> str;
    rev = str;
    reverse(rev.begin(), rev.end());
    if (str == rev)
        cout << str << " is a palindrome" << endl;
    else
        cout << str << " is not a palindrome" << endl;
    return 0;
}
This method reverses the string and compares it to the original, which is simple but uses extra space.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks up to half the string length, so it runs in linear time O(n), where n is the string length.

Space Complexity

It uses constant extra space O(1) because it only stores a few variables and does comparisons in place.

Which Approach is Fastest?

The two-pointer and loop comparison methods are fastest with O(1) space, while reversing the string uses extra space and may be slower.

ApproachTimeSpaceBest For
Loop comparisonO(n)O(1)Memory efficient, fast
Two pointersO(n)O(1)Clear logic, efficient
String reverseO(n)O(n)Simple code, uses extra space
💡
Use a loop to compare characters from both ends moving towards the center for an efficient palindrome check.
⚠️
Beginners often forget to stop the comparison at the middle, causing unnecessary checks or errors.