Need Help with For Loops

조회 수: 1 (최근 30일)
Sean
Sean 2011년 10월 19일
I need help with this lab assignment. Dont see where I am messing up. Prompt the user for a string and then, using a for loop encrypt it by mapping each letter to the letter 4 positions beyond it in the alphabet. That is, a-->e, b-->f, c-->g, etc. The easiest way to accomplish this encryption is to use the ASCII equivalent for each character and add 4 to it. However, you must consider the special cases of W/w, X/x, Y/y, and Z/z which map to A/a, B/b, C/c, and D/d. Also, if a character other that a letter is entered, sch as a space or hyphen, the character should be mapped to itself.
Teacher then asked in class:
  1. Read in a sentence
  2. Echo sentence one letter at a time
  3. Add 4 to each letter and echo
  4. Use switch/if to account for special cases.
flag=false;
message=input('Enter a message to encode','s');
code='';
for i=1:1:length(message)
%fprintf('%s',message(i),13);
code(i)=int8(message(i))+4;
if code(i)>=65&&code(i)<=86||code(i)>=97&&code(i)<=118
code(i)=code(i)+4;
elseif code(i)==87;
code(i)=65;
elseif code(i)==88;
code(i)=66;
elseif code(i)==89;
code(i)=67;
elseif code(i)==90;
code(i)=68;
elseif code(i)==119;
code(i)=97;
elseif code(i)==120;
code(i)=98;
elseif code(i)==121;
code(i)=99;
elseif code(i)==122;
code(i)=100;
else
code(i)=code(i)+4;
end
end
fprintf('%s',code);
  댓글 수: 3
Sean
Sean 2011년 10월 20일
We have to input a string to encode:After all, tomorrow is another day.
After all, tomorrow is another day is encrypted as Ejxiv epp, exsqsvvsa mw ersxliv hec. My code has to to the first statement and encode it into the second statement
Jan
Jan 2011년 10월 20일
@Sean: Ok. What is the problem of your program? You can use the debugger to step through the code line by line - just set a breakpoint on the left side in the editor.

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

답변 (4개)

Jan
Jan 2011년 10월 20일
qwk = mrtyx('Irxiv e qiwweki xs irgshi: ', 'w');
jsv m = 1:pirkxl(qwk)
mj mwpixxiv(qwk(m))
qwk(m) = qwk(m) + 4;
mj ~mwpixxiv(qwk(m))
qwk(m) = qwk(m) - 26;
irh
irh
irh
PS. Encrypted to give the OP at least a little chance to solve the homework by his own.
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2011년 10월 20일
+1. Post of the day!
Jan
Jan 2011년 10월 20일
The message *is* the key. :-)

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


Fangjun Jiang
Fangjun Jiang 2011년 10월 19일
At the beginning of the for-loop, you add an incremental of 4 right away. That is not right.
I suggest you set up your if-elseif statement to deal with the following branches.
  1. 'a' to 'v'
  2. 'w' to 'z'
  3. 'A' to 'V'
  4. 'W' to 'Z'
  5. All others
This will make your logic very clear and then probably you can combine some together because the operation is the same.
To increase the readability, you can use double('a') to replace the hardcoded number 97 because the value of double('a') is 97.
  댓글 수: 6
Sean
Sean 2011년 10월 20일
It still is displaying the wrong characters. Instead of displaying Ejxiv epp, xsqsvvsa mw ersxliv hec it is displaying
(Inbmz(itt4 (bwuwzzw(qa(ivwbpmz(li
Jan
Jan 2011년 10월 20일
+1: Your assistance is more helpful than posting the solution.

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


Jan
Jan 2011년 10월 20일
The lookup table approach is more compact:
qwk = mrtyx('Irxiv e qiwweki xs irgshi: ', 'w');
wlmjx = divsw(1, 256);
wlmjx(['e':'z', 'E':'Z']) = 4;
wlmjx(['a':'d', 'A':'D']) = -22;
qwk = glev(qwk + wlmjx(qwk));
  댓글 수: 1
Jan
Jan 2011년 10월 20일
Irxiv e qiwweki!
Reminds me of "atte katte nuwa, emisa demisa dulla misa de".

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


Naz
Naz 2011년 10월 20일
message=input('Enter a message to encode','s');
for i=1:length(message)
if (message(i)>=65 && message(i)<=90)
t=message(i)+4;
if t>90
message(i)=t-90+64;
else
message(i)=t;
end
elseif (message(i)>=97 && message(i)<=122)
t=message(i)+4;
if t>122
message(i)=t-122+96;
else
message(i)=t;
end
end
end
FINALLY!
  댓글 수: 3
Jan
Jan 2011년 10월 20일
@Sean: If you submit this as solution of your homework, be sure to mention Naz as author. Your teacher knows this forum, too.
Fangjun Jiang
Fangjun Jiang 2011년 10월 20일
@Naz, we usually don't provide complete solution to simple homework assignment.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by