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.

22 次查看(过去 30 天)
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.

采纳的回答

John D'Errico
John D'Errico 2020-3-18
编辑:John D'Errico 2020-3-18
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
  4 个评论
John D'Errico
John D'Errico 2020-3-18
You wrote a double loop, which is not the same. You might think of that double loop as essentially comparing all elements of the string with all other elements. When you have nested for loops like that, they are not running concurrently. One runs inside the other, so you have n^2 comparisons, even though one of them is running backwards.
Guillaume
Guillaume 2020-3-18
I would strongly recommend using isstrprop as I answered instead of ismember(str,'abcdefghijklmnopqrstuvwxyz'), at least if you don't want to offend the majority of languages (pretty much anything but english) that have more letters than just a:z.
>> s = char(224:246)
s =
'àáâãäåæçèéêëìíîïðñòóôõö'
>> isstrprop(s, 'alpha') %Indeed they're all letters
ans =
1×23 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

请先登录,再进行评论。

更多回答(2 个)

Guillaume
Guillaume 2020-3-18
  4 个评论
James Metz
James Metz 2020-3-18
编辑:James Metz 2020-3-18
gotcha! that makes sense. Sorry I'm new to MATLAB so most of this stuff just goes right over my head.
Ok so this is what I have so far, but its still providing a false answer when I input... 'was it a car or a cat I saw?'
function palindrome = hw4_problem2(v)
palindrome = true;
l = lower(v);
m = l(isstrprop(l, 'alpha'));
n = length(m);
for ii = 1:n
for jj = n:-1:1
if m(ii) ~= m(jj)
palindrome = false;
else
palindrome = true;
end
end
end
end
What am I missing?
Steven Lord
Steven Lord 2020-3-18
That checks each element of m against each element of m, but then throws away each result in turn as it checks the next pair of elements. This is only really checking the last element (ii equal to n) and the first element (jj equal to 1.)
You don't want that; you only want to check each element against the corresponding element "at the other end" of m. You only need one for loop, but you do need to stop the loop or treat the value of palindrome as somewhat "sticky" (once you fail one check and know that the string is not a palindrome, no future check can undo that failure and make it a palindrome again.)
The Tips on the documentation page for the for keyword may be of use to you, as might the functions any, all, and, and/or or. Of if the assignment doesn't require you to use a loop, see John D'Errico's answer.

请先登录,再进行评论。


James Metz
James Metz 2020-3-18
For anyone looking for the right code for this question I have attached the code that worked:
function palindrome = hw4_problem2(v)
l = lower(v);
m = l(ismember(l, 'abcdefghijklmnopqrstuvwxyz'));
if isequal(m, flip(m)) == 0
palindrome = false;
else
palindrome = true;
end
end
Thanks to everyone who helped me get there!
Best.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by