How do you write a matlab function that identifies if a string is a palindrome or not?

25 ビュー (過去 30 日間)
So far I've got ...
string = input('What is your number: '); disp(fliplr(string))
if string == fliplr(string) disp('Your number is a PALINDROME') else disp('Your number is NOT A PALINDROME') end
And now I'm stuck

採用された回答

Image Analyst
Image Analyst 2014 年 3 月 30 日
Almost. Try this:
string = input('What is your string: ', 's');
fprintf('Your string = %s\nFlipped, it = %s\n', string, fliplr(string));
if string == fliplr(string)
disp('Your number is a PALINDROME')
else
disp('Your number is NOT A PALINDROME')
end
  4 件のコメント
Star Strider
Star Strider 2014 年 3 月 30 日
That’s my all time fave!
Probably need to remove all blanks as well before flipping and doing the comparison.
Image Analyst
Image Analyst 2014 年 3 月 30 日
True.
string = lower(string);
string(string == ' ') = '';

サインインしてコメントする。

その他の回答 (3 件)

sesha sai
sesha sai 2020 年 9 月 26 日
function [ palindromic ] = palindrome( string )
if length(string) < 1
palindromic = logical(1);
return;
end
if string(1) ~= string(length(string))
palindromic = logical(0);
return;
end
palindromic = palindrome(string(2:length(string)-1));
end

Benjamin Askelund
Benjamin Askelund 2016 年 10 月 19 日
編集済み: Benjamin Askelund 2016 年 10 月 19 日
Hi, I made a code that flipps as well NOT USING fliplr based on "Image Analyst" code. Lots of people ask for this as far as I know
clc
%Ask user for string
A = input('What is your string: ', 's');
%flipp string
B = flip(A);
fprintf('Your word = %s\nflipped word is = %s\n', A, B);
%compare stringA and FlippedString (bool)
if strcmp(A, B) == 1
disp('Your number is a PALINDROME')
else
disp('Your number is NOT A PALINDROME')
end
This code also works for numbers. Just type numbers insted of strings
Sorce: https://se.mathworks.com/help/matlab/ref/flip.html
Signed BDA
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 10 月 19 日
fliplr(A) just calls flip(A,2)
Steven Lord
Steven Lord 2016 年 10 月 19 日
Lots of people ask for this because it's a fairly easy homework assignment and so it is one that's often assigned in courses where students are being introduced to MATLAB.

サインインしてコメントする。


Bhuvana Krishnaraj
Bhuvana Krishnaraj 2020 年 1 月 9 日
編集済み: Walter Roberson 2024 年 1 月 10 日
x=input('Enter number: ');
temp=x;
y = 0;
while x > 0
t = mod(x,10);
y = 10*y+t;
x = (x-t)/10; %% without this it will give floating value
end
if(y==temp)
disp('The number is palindrome');
else
disp('The number is not palindrome');
end
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 9 月 26 日
Suppose the user enters 050. That would certainly appear to be a palindrome. You use input() with no 's' to read it so you get a binary double precision value equal to decimal 50. You proceed to process it numerically and get 10*0 + 5 = 5. But 5 is not 50 so you say No it is not a palindrome. When it should probably be one.

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by