Interview Question

Interview

Write a function to determine if a string is a palindrome.

Answer

Interview Answer

2 Answers

1

bool palindrom(char* s, int length) { int l = 0; int r = length - 1; while (l < r) { if (s[l] != s[r]) return false; ++l; --r; } return true; }

Anonymous on Feb 2, 2013
2

Using the definition: "A Palindrome is a string where the first and last character are the same, and the middle is a palindrome", the code would look like this bool is_palindrome(char *string, int length) { if(length == 1 || length == 0) return true; if(string[0] == string[length-1]) return is_palindrome(string+1,length-2); else return false; }

Anonymous on Feb 16, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.