フィルターのクリア

How to make a Palindrome Checker

5 ビュー (過去 30 日間)
Krish Desai
Krish Desai 2015 年 12 月 5 日
コメント済み: Image Analyst 2023 年 5 月 1 日
I need to write a code that checks to see if an input is a palindrome-same backwards as forwards, for instance madam or rotor.
I need to use the "programming method" which means I can't use any functions with str, eval, flip or printf. I don't want the answer, but just a place to start on how to build this code. Note, this isn't homework, just a practice question.

採用された回答

Image Analyst
Image Analyst 2015 年 12 月 5 日
I get no such problem. Did you pass in rotor or 'rotor'? You probably forgot the quotes around it, because this works fine for me
output = isPalindrome('rotor')
If the function is
function output = isPalindrome(yourString)
lastIndex=floor(length(yourString)/2);
for k=1:lastIndex
if yourString(k)~=yourString(end+1-k)
output = false;
else
output = true;
end
end
You also didn't use the debugger like I recommended or else you would have found out that you need to use (end+1-k) instead of (lastIndex+1-k).
  8 件のコメント
Kashiraj
Kashiraj 2023 年 5 月 1 日
can anybody expain palindrom program in detail
Image Analyst
Image Analyst 2023 年 5 月 1 日
@Kashiraj it simply compares the first letter to the last letter, the second letter to the next to the last letter, and so on. Just think how you would describe it yourself if you had to explain it to someone.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 12 月 5 日
The first point is X(1). The last point is X(end)
The second point is X(2). The second last point is X(end-1)
The third point is X(3). The third last point is X(end-2)
So for the K'th point, which point is the K'th last point?
  9 件のコメント
Krish Desai
Krish Desai 2015 年 12 月 5 日
I'm trying the following but when I input isPalindrome(rotor) the error message "Undefined function or variable 'rotor'." pops up.
function output=isPalindrome(yourString)
lastIndex=floor(length(yourString)/2);
for k=1:lastIndex
if yourString(k)~=yourString(lastIndex+1-k)
output='false';
else
output='true';
end
end
Walter Roberson
Walter Roberson 2015 年 12 月 5 日
isPalindrome('rotor')

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

カテゴリ

Help Center および File ExchangeString についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by