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 ?

조회 수: 2 (최근 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에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by