Why the function doesnt work with('1234')??!
显示 更早的评论
function coded=caesar(str,shift) a=double(str); counter=1; coded=zeros(1,length(str)); for i=[1:length(str)] if a(1,couner)+shift>126||a(1,counter)+shift<32%j while a(1,couner)+shift>126 coded(1,counter)=a(1,counter)+shift-95; counter=counter+1; end while a(1,counter)+shift<32 coded(1,counter)=a(1,counter)+shift+95; counter=counter+1; end else coded(1,counter)=a(1,counter)+shift; counter=counter+1; end end coded=char(coded) end
7 个评论
Walter Roberson
2020-2-5
Are you failing to pass in the shift as well?
Marco Nashaat
2020-2-5
Walter Roberson
2020-2-5
Please post your code in readable form. Use "smart indent" in the MATLAB editor first. Then here, click on the '>' button in the 'CODE' section of the editing toolbar, and copy and paste your code.
Marco Nashaat
2020-2-6
编辑:Walter Roberson
2020-2-6
Walter Roberson
2020-2-6
Have a closer look at your lines 6 and 7: you use a variable named couner
Marco Nashaat
2020-2-7
Marco Nashaat
2020-2-7
回答(1 个)
JESUS DAVID ARIZA ROYETH
2020-2-5
solution:
save it :
function coded=caesar(str,shift)
a=double(str);
counter=1; coded=zeros(1,length(str));
for i=[1:length(str)]
if a(1,counter)+shift>126||a(1,counter)+shift<32%j
while a(1,couner)+shift>126
coded(1,counter)=a(1,counter)+shift-95;
counter=counter+1;
end
while a(1,counter)+shift<32
coded(1,counter)=a(1,counter)+shift+95;
counter=counter+1;
end
else
coded(1,counter)=a(1,counter)+shift;
counter=counter+1;
end
end
coded=char(coded);
end
and type it in the Command Window :
caesar('1234',2)
4 个评论
Walter Roberson
2020-2-5
JESUS DAVID ARIZA ROYETH: we discourage people from providing complete solutions for homework assignments.
Stephen23
2020-2-5
Simpler than lots of loops: use mod.
JESUS DAVID ARIZA ROYETH
2020-2-5
Walter Roberson
2020-2-6
So it is, compete with the user of couner as a variable at one point.
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!