MATLAB: I’m trying to create a code that determines whether a word (or phrase) is a palindrome. The code only needs to look at the actual letters within the input string, but I’m not sure how to do that.

for looploopspalindrome

The jist of the problem is to create a function that determines whether a string input is a palindrome by returning the logical true or false. The function needs to disregard any non-letter (i.e. punctuation, spaces, capitalization, etc.), but I'm not sure how to do this.
Here is my code:
function palindrome = hw4_problem2(v)
palindrome = true;
l = lower(v);
n = length(v);
if mod(n, 2) == 0
for ii = 1:n
for jj = n:-1:1
if l(ii) ~= l(jj)
palindrome = false;
else
palindrome = true;
end
end
end
end
end
I was able to use "lower" to get everything into lower letter form, but how do I disregard nonletters? Thanks.

Best Answer

Well, you did show some effort, actually a credible one at that. :) You already know how to use tools like lower, so try a few others.
str = 'The quick Brown fox jumped over the lazy dog.';
str = lower(str);
str = str(ismember(str,'abcdefghijklmnopqrstuvwxyz'))
str =
'thequickbrownfoxjumpedoverthelazydog'
Now how can you tell if it is a palindrome? Have you considered flip?
isequal(str,flip(str))
ans =
logical
0
Related Question