How to make a Palindrome Checker
Show older comments
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
More Answers (1)
Walter Roberson
on 5 Dec 2015
1 vote
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
Krish Desai
on 5 Dec 2015
Krish Desai
on 5 Dec 2015
Walter Roberson
on 5 Dec 2015
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?
Krish Desai
on 5 Dec 2015
Image Analyst
on 5 Dec 2015
Here's a hint/start:
lastIndex = floor(length(yourString)/2);
for k = 1 : lastIndex
if yourString(k) ~= your.......
break; % No match, not a palindrome.
Krish Desai
on 5 Dec 2015
Image Analyst
on 5 Dec 2015
Krish Desai
on 5 Dec 2015
Walter Roberson
on 5 Dec 2015
isPalindrome('rotor')
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!