Check if a palindrome.

123 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2014 年 4 月 2 日
編集済み: DGM 2022 年 3 月 8 日
I have the script:
%1. Clear the memory and command window.
clear
clc
%2. Input a string.
string = input('What is your string: ', 's');
%3. Check if the string is a palindrome or not.
if string == fliplr(string);
disp (['The string, ' string ', is a palindrome.'])
else
disp (['The string, ' string ', is not a palindrome.'])
end
I want to make it so if the user enters anything but a word for the string it does not run and maybe instead of flipping the string comparing each cell. e.g if letter 1 is the same as the last letter, if letter 2 is the same as the second last letter. This method would be prefered. Also how would i make it so that that i could enter a cell array of striings and a vector of logical values was returned.

採用された回答

Image Analyst
Image Analyst 2014 年 4 月 2 日
Looks almost identical to a question I answered a few days ago http://www.mathworks.com/matlabcentral/answers/123671#answer_131334. You need to cast to lower case and remove spaces. You can use inputdlg() to ask the user for several strings at a time and get a cell array.

その他の回答 (1 件)

RAGHAVENDRA NAIK
RAGHAVENDRA NAIK 2022 年 3 月 8 日
編集済み: DGM 2022 年 3 月 8 日
sum=0;
n=input('Tell me a number \n');
count=input('no. of digits\n')
x=n;
if (count>=2)
for i=1:n
if (n>0)
r=round(rem(n,10)-0.1);
sum=sum*10+r;
n=round((n/10)-0.1);
c=x-sum;
else
break
end
end
if (c==0)
disp('the no. is palindrome');
else
disp('the no. is not palindrome');
end
else
disp('the no. is not palindrome');
end

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by