필터 지우기
필터 지우기

Need to utilise only letter ASCII in Caesar Cipher Code

조회 수: 3 (최근 30일)
Bozza
Bozza 2022년 9월 11일
편집: Bozza 2022년 10월 7일
Hello,
I am currently creating a Caesar cipher code and need help, please:
  1. A caesar code reverses letter order and capitalisation.
I have written a short code below, which performs the shift correctly, however, I am unsure how to reverse capitalisation, and only edit the letters.
function [out_str] = str_edit(in_str, shift)
char_set = char(32):char(126);
[out_str] = char(in_str+shift);
end

답변 (1개)

Walter Roberson
Walter Roberson 2022년 9월 11일
Hint:
T('A':'Z') = circshift('a':'z', count)
encoded = T(msg)
  댓글 수: 2
Bozza
Bozza 2022년 9월 13일
Thanks for helping. I'm quite unfamiliar with Matlab, so I am unsure how to implement this into my code?
Walter Roberson
Walter Roberson 2022년 9월 13일
Example:
T = char(1):char(255);
T('A':'C') = 'bca';
T('a':'c') = 'BCA';
msg = 'Call a cab!'
msg = 'Call a cab!'
T(msg)
ans = 'aBll B ABC!'
What was uppercase A to C become lower-case and also shifted by 1. What was lowercase a to c become upper-case and also shifted by 1. Things outside of A B C a b c are not affected.
If you calculate the replacement vector based upon your shift count, then the indexing of T with the msg does all of the case changing and shifting at the same time, without any need of a for loop.

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

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by