Borrar filtros
Borrar filtros

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 visualización (últimos 30 días)
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).

Respuestas (1)

Jim Riggs
Jim Riggs el 12 de Nov. de 2018
Editada: Jim Riggs el 12 de Nov. de 2018
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 comentarios
Guillaume
Guillaume el 13 de Nov. de 2018
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 el 13 de Nov. de 2018
Editada: Jim Riggs el 13 de Nov. de 2018
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)

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by