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
Walter Roberson 2020년 2월 5일
Are you failing to pass in the shift as well?
Marco Nashaat
Marco Nashaat 2020년 2월 5일
I tried 'ABCD' with shift=3 And it worked but it didn't work with '1234'and shift=96
Walter Roberson
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
Marco Nashaat 2020년 2월 6일
편집: Walter Roberson 2020년 2월 6일
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
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
Walter Roberson
Walter Roberson 2020년 2월 6일
Have a closer look at your lines 6 and 7: you use a variable named couner
Marco Nashaat
Marco Nashaat 2020년 2월 7일
Wow how stupid of me!! Thank you very much
Marco Nashaat
Marco Nashaat 2020년 2월 7일
I tried it now but it seems like it's not working yet,I got this message...Index in position 2 exceeds array bounds (must not exceed 4).
Error in caesar (line 7) while a(1,counter)+shift>126 Please help!!

댓글을 달려면 로그인하십시오.

답변 (1개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2020년 2월 5일

0 개 추천

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
Walter Roberson 2020년 2월 5일
JESUS DAVID ARIZA ROYETH: we discourage people from providing complete solutions for homework assignments.
Stephen23
Stephen23 2020년 2월 5일
Simpler than lots of loops: use mod.
Walter Roberson sorry but it is not my solution is the same function provided by Marco Nashaat
Walter Roberson
Walter Roberson 2020년 2월 6일
So it is, compete with the user of couner as a variable at one point.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2020년 2월 5일

댓글:

2020년 2월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by