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

155 views (last 30 days)

Show older comments

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

##### 0 Comments

### Accepted Answer

Image Analyst
on 30 Mar 2014

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 Comments

### More Answers (5)

sesha sai
on 26 Sep 2020

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

##### 1 Comment

Benjamin Askelund
on 19 Oct 2016

Edited: Benjamin Askelund
on 19 Oct 2016

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 Comments

Steven Lord
on 19 Oct 2016

Bhuvana Krishnaraj
on 9 Jan 2020

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 Comment

Walter Roberson
on 26 Sep 2020

Sizaka karki
on 8 Nov 2021

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 Comment

Image Analyst
on 8 Nov 2021

Sizaka karki
on 8 Nov 2021

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 Comment

Image Analyst
on 8 Nov 2021

### See Also

### Community Treasure Hunt

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

Start Hunting!