MATLAB: How to make a Palindrome Checker

palindromestring

I need to write a code that checks to see if an input is a palindrome-same backwards as forwards, for instance madam or rotor.
I need to use the "programming method" which means I can't use any functions with str, eval, flip or printf. I don't want the answer, but just a place to start on how to build this code. Note, this isn't homework, just a practice question.

Best Answer

I get no such problem. Did you pass in rotor or 'rotor'? You probably forgot the quotes around it, because this works fine for me
output = isPalindrome('rotor')
If the function is
function output = isPalindrome(yourString)
lastIndex=floor(length(yourString)/2);
for k=1:lastIndex
if yourString(k)~=yourString(end+1-k)
output = false;
else
output = true;
end
end
You also didn't use the debugger like I recommended or else you would have found out that you need to use (end+1-k) instead of (lastIndex+1-k).
Related Question