Article #3 in a 9-part series.
Since we had tested anagrams , it seems fair we give the palindrome check problem a go. How to check if a string is a palindrome? My solution is shown at the bottom in C#, Java, Objective-C, Swift (updated for 5), and C++.
observations
By definition a palindrome is word or phrase that is the same reversed ignoring lettercase, spaces, and punctuation. Other symbols and numbers count. Some examples:
Madam, I’m Adam.
Never odd or even.
Rise to vote, sir!
Salas
191
assumptions
Let’s assume a single-character word is a palindrome. An empty string, or string containing only punctuation, wouldn’t be a word or phrase, so we won’t include them.
program
Remember to check for valid input. Like in the anagram test, we’ll use a regular expression to remove punctuation. A simple method is to iterate from the beginning of the string, and for each character check to see if the mirrored position from the end is the same character.
C#
C#: is the string a palindrome? static bool IsPalindrome ( string phrase )
// remember to check for null
if ( phrase != null && phrase . Length > 0 )
// spaces and punctuation don't count, but include symbols
phrase = System . Text . RegularExpressions . Regex . Replace ( phrase , "[\\s\\p{P}]" , "" );
// we could also compare ignoring case during the for-loop below.
phrase = phrase . toLower ();
// iterate to the midpoint and check if char same as
// on mirrored from end position
for ( int i = 0 ; i < len / 2 + 1 ; ++ i )
if ( phrase [ i ] != phrase [ len - i - 1 ])
Java
Java: is the string a palindrome? public class StringProblems
static boolean isPalindrome ( String phrase )
if ( phrase != null && phrase . length () > 0 ) {
// spaces and punctuation don't count
// replace using regular expression
String pattern = "[\\s\\p{P}]" ;
phrase = phrase . replaceAll ( pattern , "" );
phrase = phrase . toLowerCase ();
int len = phrase . length ();
for ( int i = 0 ; i < len / 2 + 1 ; ++ i ) {
if ( phrase . charAt ( i ) != phrase . charAt ( len - i - 1 )) {
Objective-C
Objective-C: is the string a palindrome? + ( BOOL ) isPalindrome: ( NSString * ) phrase
// remove spaces, punctuation
NSRegularExpression * regex = [ NSRegularExpression regularExpressionWithPattern : @"[ \\ s \\ p{P}]"
options : NSRegularExpressionCaseInsensitive error : & error ];
phrase = [ regex stringByReplacingMatchesInString : phrase
range : NSMakeRange ( 0 , phrase . length )
phrase = [ phrase lowercaseString ];
// go to mid-point while checking for same character on other end
for ( int i = 0 ; i < phrase . length / 2 + 1 ; ++ i ) {
if ([ phrase characterAtIndex : i ] != [ phrase characterAtIndex :( phrase . length - i - 1 )]) {
Swift 5
Swift 5.2: is the string a palindrome? var stringList : [ String ] = [ "Tom Marvolo Riddle" , "School master" ]
stringList . append ( "I am Lord Voldemort!" )
stringList . append ( "the classroom" )
stringList . append ( "Madam, I'm Adam." )
stringList . append ( "Ray-Adverb" )
stringList . append ( "Ray adverb" )
stringList . append ( "Never odd, or even?" )
stringList . append ( "Astronomer!!" )
stringList . append ( "moon starer?" )
stringList . append ( "Dave Barry" )
if ( isPalindrome ( phrase1 : str1 )) {
print ( "'" + str1 + "' is a palindrome" )
func isPalindrome ( phrase1 : String ) -> Bool {
// define regular expression to remove spaces and punctuation
let regex = try ! NSRegularExpression ( pattern : "[ \\ s \\ p{P}]" , options : [. caseInsensitive ])
let trimmedPhrase = regex . stringByReplacingMatches ( in : phrase1 , options : [], range : NSMakeRange ( 0 , phrase1 . count ), withTemplate : "" )
let len = trimmedPhrase . count
// make a lower-case character array to compare
let chArr1 = Array ( trimmedPhrase . lowercased ())
for i in 0 .. < len / 2 + 1 {
if ( chArr1 [ i ] != chArr1 [ len - i - 1 ]) {
C++11 console
C++11: is the string a palindrome? void TestString ( std :: string str );
bool IsPalindrome ( std :: string phrase );
TestString ( "Never odd or even?" ); // true
TestString ( "Never odd or evan!" ); // false
TestString ( "Madam, I'm Adam!" ); // true
void TestString ( std :: string str )
std :: cout << "'" << str << "' is a palindrome. \n " ;
bool IsPalindrome ( std :: string phrase )
// spaces and punctuation don't count
// (see http://www.cplusplus.com/reference/regex/ECMAScript/ for patterns)
std :: regex ex ( "[ \\ s[:punct:]]" );
phrase = std :: regex_replace ( phrase , ex , "" );
std :: transform ( phrase . begin (), phrase . end (), phrase . begin (), :: tolower );
int len = phrase . length ();
// iterate to middle and check of char is same on mirrored side
for ( int i = 0 ; i < len ; i ++ )
if ( phrase [ i ] != phrase [ len - i - 1 ])
Article #3 in a 9-part series.