write a function called palindrome that takes one input argument a char vector and recursively determine whether that argument is a palindrome you are not allowed to use loops not built in function like srtcmp etc. the function returns true or false
10 ビュー (過去 30 日間)
古いコメントを表示
JAYANTHI SANKARALINGAM
2020 年 10 月 21 日
回答済み: Tarun Sangwan
2024 年 3 月 27 日
function ok=palindrome(txt)
iflength(txt)<=1
ok=true;
else
ok=(txt(1)==txt(end)&&palindrome(txt(2:end-1)));
end
end
hi,
It shows error in else statment in the above program, kindly give me a solution to solve the above problem.
0 件のコメント
採用された回答
Ameer Hamza
2020 年 10 月 21 日
編集済み: Ameer Hamza
2020 年 10 月 21 日
There should be a space between if keyword and the condition
if length(txt)<=1
%^ insert a space here
Apart from that, the logic is correct, but you just have a misplaced bracket. Following is correct
ok=(txt(1)==txt(end))&&palindrome(txt(2:end-1));
その他の回答 (3 件)
Sandeep Kumar Patel
2022 年 4 月 13 日
編集済み: DGM
2024 年 1 月 10 日
function ok=palindrome(txt)
if length(txt)<=1 % added space
ok = true;
else
ok = (txt(1)==txt(end)) && palindrome(txt(2:end-1));
% close parentheses --^
end
end
1 件のコメント
DGM
2024 年 1 月 10 日
Editor's note: I added the comments here so that readers know the two lone characters that were actually changed.
Black Woods
2022 年 12 月 12 日
function ans=palindrome(v)
if v(1)==v(end)
ans=true;
if length(v)==1 || length(v)==2
return
else
palindrome(v(2:length(v)-1));
end
else
ans=false;
end
end
0 件のコメント
Tarun Sangwan
2024 年 3 月 27 日
function p = palindrome(n)
if length(n)/2<1
p = 2
else
p = n(1) == n(end)
p = [p palindrome(n(2:end-1))]
end
p = ~ismember(0,p)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Simulink Environment Customization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!