How to make a Palindrome Checker

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.

 Accepted 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).

8 Comments

sorry, I didn't see your post about the debugger.
I have another problem though, what if I input punctuation, for instance: a car, a man, a maraca. Is there a function that eliminates all the punctuation?
For example, to get rid of commas:
yourString(yourString==',') = []; % Set commas to null
Now commas will be removed and the string will have fewer characters if any commas were in there.
Hint: you do not just want to get rid of punctuation: you want to get rid of all non-letters (unless perhaps you allow digits in the palindrome)
How do it without using loops and built in functions??
Which builtin functions are you trying to avoid? And since that sounds like homework: what did you try yourself?
can anybody expain palindrom program in detail
@Kashiraj it simply compares the first letter to the last letter, the second letter to the next to the last letter, and so on. Just think how you would describe it yourself if you had to explain it to someone.

Sign in to comment.

More Answers (1)

The first point is X(1). The last point is X(end)
The second point is X(2). The second last point is X(end-1)
The third point is X(3). The third last point is X(end-2)
So for the K'th point, which point is the K'th last point?

9 Comments

Wouldn't it just be K?
Or it could be K+1
Suppose K = 2. Then that is the second point, X(2). Is the second last point X(K) = X(2) also?
Suppose the string is length 6. Then
X(1) needs to match X(6)
X(2) needs to match X(5)
X(3) needs to match X(4)
What is the total of the two indices in each case? How does that relate to the length of the string?
I understand now, I thought you were referring to k being the final point, or in this case the middle point. The total of the indices is string length+1.
Here's a hint/start:
lastIndex = floor(length(yourString)/2);
for k = 1 : lastIndex
if yourString(k) ~= your.......
break; % No match, not a palindrome.
yourString(lastIndex+1-k)?
I guess you have not seen this link. Try your guess and see - it's easy enough to do.
I'm trying the following but when I input isPalindrome(rotor) the error message "Undefined function or variable 'rotor'." pops up.
function output=isPalindrome(yourString)
lastIndex=floor(length(yourString)/2);
for k=1:lastIndex
if yourString(k)~=yourString(lastIndex+1-k)
output='false';
else
output='true';
end
end

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!