I need to write a program that defines whether a string is a PALINDROME or not WITHOUT using fliplr, reverse or similar commands. Can anyone help with that ?

1 次查看(过去 30 天)
A palindrome is a word that remains the same when all of its letters are reversed: for example, ‘radar’ and ‘tenet’ are both palindromes. Write a program which asks the user to input a word and then checks whether it is a palindrome or not, and prints ‘True’ when it is, and ‘False’ when it is not. Do not use ‘fliplr’ (or similar commands/functions to ‘fliplr’ like ‘reverse’). Your program should make some checks whether the input is appropriate (e.g. whether the input is of character type or not).

回答(1 个)

Jim Riggs
Jim Riggs 2018-11-12
编辑:Jim Riggs 2018-11-12
A palindrome is a word or phrase that is exactly the same forward and bacward. I would use a loop to compare the first character to the last character, then the second character to the next to last character, etc. If all characters match like this, then it is a palindrome. (Note that you need to eliminate whte spaces from the string first)
  4 个评论
Guillaume
Guillaume 2018-11-13
The check can easily be carried out in one line without using any loop or function other than basic indexing and isequal. However, since this is homework, whose whole point is for the student to come up with their own algorithm, I'm not going to show how.
I just wanted to point out that using string as a variable name is a very bad idea, since it's also a built-in function (since R2016b).
Jim Riggs
Jim Riggs 2018-11-13
编辑:Jim Riggs 2018-11-13
I just checked google for some examples of palendromes, and ones that use multiple words also may contain punctuation, (which is ignored). So, step 3 should test for any non-alphabetic character and eliminate it from the string, not just white space) (see Matlab command isletter)

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by