This problem is asking you to shift the character variable by a given element n
the word wrap means that if the ASCII code of your character exceeds 32 or 126 you need to circle back again .
For example
if ASCII code is 97 and n (shift variable) is 45 so your ASCII code is 142 which exceeds 126. So you need to subtract 126 from 142
142-126, and add the net result to 31.
you need'nt do all that..... use the function called circshift
so i defined a character array from 32 to 126 which is the required ascii range
these are the characters.
ch =
' !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
and when i use the circshift command
ch_shift_pos=circshift(ch,2)
ch_shift_pos =
'}~ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|'
ch_shift_neg=circshift(ch,-2)
ch_shift_neg=
'"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !'
Notice how the characters have shifted by 2 positions without you having to manually keep it within its range.
now if i want to find a character and its corresponding encrypted/shifted value i simply transfer the index since both ch and ch_shift are of the same size
so for example: if i want to find the encryption character of a , i first find the index of a in the 'ch' array and store it in a variable called x
and then i use that index to find the encryption in the shifted array
ch_shift_pos(x)
ans =
'_'
ch_shift_neg(x)
ans =
'c'
There! thats your answer. As simple as that. But i had to rack my brains for it. :P
16 Comments
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_716640
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_716640
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_716943
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_716943
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_717939
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_717939
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_718004
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_718004
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_719748
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_719748
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_811298
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_811298
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_825063
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_825063
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_825073
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_825073
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_825196
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_825196
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_825447
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_825447
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_836182
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_836182
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_837360
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_837360
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_851638
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_851638
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_872711
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_872711
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_1028179
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_1028179
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_1028422
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/466636-caesar-cyphor-encryption-problem#comment_1028422
Sign in to comment.