String Reversal and Palindrome
In this page:
String Reversal
Reversing a string means producing a new string with the same characters in the opposite order, from last character to first, which can be done by building a new string from the end backward or by swapping characters in place.
Example: String Reversal
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s = "hello";
reverse(s.begin(), s.end());
cout << "Reversed: " << s << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "hello";
String reversed = new StringBuilder(s).reverse().toString();
System.out.println("Reversed: " + reversed);
}
}
s = "hello"
print("Reversed:", s[::-1])
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "hello";
int n = strlen(s);
for (int i = 0; i < n / 2; i++) {
char t = s[i]; s[i] = s[n - 1 - i]; s[n - 1 - i] = t;
}
printf("Reversed: %s\n", s);
return 0;
}
Login to try C/C++/Java code in the editor
Palindrome Basics
A palindrome is a string that reads identically forwards and backwards, like level or racecar. Checking for one means comparing the string against its own reverse, or comparing characters from both ends inward.
Example: Palindrome Basics
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s = "racecar";
string rev = s;
reverse(rev.begin(), rev.end());
cout << (s == rev ? "Palindrome" : "Not a palindrome") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "racecar";
String rev = new StringBuilder(s).reverse().toString();
System.out.println(s.equals(rev) ? "Palindrome" : "Not a palindrome");
}
}
s = "racecar"
print("Palindrome" if s == s[::-1] else "Not a palindrome")
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "racecar";
int n = strlen(s), isPal = 1;
for (int i = 0; i < n / 2; i++) if (s[i] != s[n - 1 - i]) isPal = 0;
printf("%s\n", isPal ? "Palindrome" : "Not a palindrome");
return 0;
}
Login to try C/C++/Java code in the editor
Two Pointer Method
The two-pointer method checks a palindrome without ever building a reversed copy: one pointer starts at the front, one at the back, and they compare characters while moving toward the middle, stopping early the moment a mismatch is found.
Example: Two Pointer Method
#include <iostream>
using namespace std;
int main() {
string s = "level";
int left = 0, right = s.length() - 1;
bool isPal = true;
while (left < right) {
if (s[left] != s[right]) { isPal = false; break; }
left++; right--;
}
cout << (isPal ? "Palindrome" : "Not a palindrome") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "level";
int left = 0, right = s.length() - 1;
boolean isPal = true;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) { isPal = false; break; }
left++; right--;
}
System.out.println(isPal ? "Palindrome" : "Not a palindrome");
}
}
s = "level"
left, right = 0, len(s) - 1
is_pal = True
while left < right:
if s[left] != s[right]:
is_pal = False
break
left += 1; right -= 1
print("Palindrome" if is_pal else "Not a palindrome")
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "level";
int left = 0, right = strlen(s) - 1, isPal = 1;
while (left < right) {
if (s[left] != s[right]) { isPal = 0; break; }
left++; right--;
}
printf("%s\n", isPal ? "Palindrome" : "Not a palindrome");
return 0;
}
Login to try C/C++/Java code in the editor
Ignoring Case
To make a palindrome check case-insensitive, convert both characters being compared to the same case (typically lowercase) before comparing, so Level is still correctly recognized as a palindrome.
Example: Ignoring Case
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string s = "Level";
int left = 0, right = s.length() - 1;
bool isPal = true;
while (left < right) {
if (tolower(s[left]) != tolower(s[right])) { isPal = false; break; }
left++; right--;
}
cout << (isPal ? "Palindrome (case-insensitive)" : "Not a palindrome") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "Level";
int left = 0, right = s.length() - 1;
boolean isPal = true;
while (left < right) {
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { isPal = false; break; }
left++; right--;
}
System.out.println(isPal ? "Palindrome (case-insensitive)" : "Not a palindrome");
}
}
s = "Level"
left, right = 0, len(s) - 1
is_pal = True
while left < right:
if s[left].lower() != s[right].lower():
is_pal = False
break
left += 1; right -= 1
print("Palindrome (case-insensitive)" if is_pal else "Not a palindrome")
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char s[] = "Level";
int left = 0, right = strlen(s) - 1, isPal = 1;
while (left < right) {
if (tolower(s[left]) != tolower(s[right])) { isPal = 0; break; }
left++; right--;
}
printf("%s\n", isPal ? "Palindrome (case-insensitive)" : "Not a palindrome");
return 0;
}
Login to try C/C++/Java code in the editor
Palindrome Practice
Palindrome problems are a great way to practice careful index management, since off-by-one errors at the string's boundaries are the most common bug, especially when also skipping non-alphanumeric characters.
Example: Palindrome Practice
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string s = "A man a plan a canal Panama";
int left = 0, right = s.length() - 1;
bool isPal = true;
while (left < right) {
if (!isalnum(s[left])) { left++; continue; }
if (!isalnum(s[right])) { right--; continue; }
if (tolower(s[left]) != tolower(s[right])) { isPal = false; break; }
left++; right--;
}
cout << (isPal ? "Palindrome" : "Not a palindrome") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "A man a plan a canal Panama";
int left = 0, right = s.length() - 1;
boolean isPal = true;
while (left < right) {
if (!Character.isLetterOrDigit(s.charAt(left))) { left++; continue; }
if (!Character.isLetterOrDigit(s.charAt(right))) { right--; continue; }
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { isPal = false; break; }
left++; right--;
}
System.out.println(isPal ? "Palindrome" : "Not a palindrome");
}
}
s = "A man a plan a canal Panama"
left, right = 0, len(s) - 1
is_pal = True
while left < right:
if not s[left].isalnum():
left += 1; continue
if not s[right].isalnum():
right -= 1; continue
if s[left].lower() != s[right].lower():
is_pal = False
break
left += 1; right -= 1
print("Palindrome" if is_pal else "Not a palindrome")
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char s[] = "A man a plan a canal Panama";
int left = 0, right = strlen(s) - 1, isPal = 1;
while (left < right) {
if (!isalnum(s[left])) { left++; continue; }
if (!isalnum(s[right])) { right--; continue; }
if (tolower(s[left]) != tolower(s[right])) { isPal = 0; break; }
left++; right--;
}
printf("%s\n", isPal ? "Palindrome" : "Not a palindrome");
return 0;
}
Login to try C/C++/Java code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: