Back In the print window choose "Save as PDF" as the destination
NOTE.CS

Palindrome String

1 topic · 28 Nov 2026
1

Palindrome String

Problem Solving
Problem: Check whether a string is a palindrome (madam).
Solution 1: Two pointer (O(1) space) · C++
string s = "madam";
int l = 0, r = s.length() - 1;
bool ok = true;
while(l < r){
    if(s[l] != s[r]){ ok = false; break; }
    l++; r--;
}
cout << (ok ? "Palindrome" : "Not Palindrome");
Solution 2: Reverse & compare · C++
string s = "madam";
string r = s;
reverse(r.begin(), r.end());
cout << (s == r ? "Palindrome" : "Not Palindrome");