countdown the received character
Afficher commentaires plus anciens
Hi guys, I have a function which will accept the characters and n, this function is a cell array with strings of decreasing lengths, from the integer n to 1. For example, when I type buildstr('a',4), the answer should be 'abcd' 'abc' 'ab' 'a'. However the code below doesnt satisfy my requirement. Can someone point out the error code?
function str=buildstr(ch,n)
temp='';
if(n<0)
str='';
else
str=cell(n,1);
for i=n:1
if(i==1)
str(i)={char(ch)};
else
temp=char(str(i+1));
str(i)={strcat(temp,char(ch))};
end
end
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!