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 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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).
0 Kommentare
Antworten (1)
Jim Riggs
am 12 Nov. 2018
Bearbeitet: Jim Riggs
am 12 Nov. 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 Kommentare
Guillaume
am 13 Nov. 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
am 13 Nov. 2018
Bearbeitet: Jim Riggs
am 13 Nov. 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)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!