Caesar's cypher is the simplest encryption algorithm. It adds a fixed value to the ASCII (unicode) value of each character of a text. In other words, it shifts the characters. Decrypting a text is simply shifting it back by the same amount, that is, it substract the same value from the characters. Write a function called caesar that accepts two arguments: the first is the character vector to be encrypted, while the second is the shift amount. The function returns the output argument coded, the encrypted text. The function needs to work with all the visible ASCII characters from space to ~. The ASCII codes of these are 32 through 126. If the shifted code goes outside of this range, it should wrap around. For example, if we shift ~ by 1, the result should be space. If we shift space by -1, the result should be ~.
for the first part of the code…this works
function coded=caesar(A,n)char_set=char(32):char(126) coded=char(A+n);end
But if i want to maintain the range between 32 and 126 ….and also wrap around the same values as asked in later half of question , it doesnt work
function coded=caesar(A,n)char_set=char(32):char(126)while A <=char_set coded=char(A+n);endend
please help me with the concerned code buiding ….( expect a simple approach , since iam a begineer)
test for these outputs
caesar('ABCD',1)ans = 'BCDE'caesar('xyz ~',1)ans = 'yz{! 'caesar('xyz ~',-1)ans = 'wxy~}' %these are correct answers to the code
Best Answer