问题描述
在matlab中,我正在编码Ceaser密码,但是空格显示为'y'字符。 如何用空格代替
case 4
disp('Breaking Ceaser Cipher')
cs = menu('Please Enter your Choice','Encryption','Decryption');
if cs==1
c = input('Enter the message: ','s');
sh = str2double(input('Enter shift: ','s'));
c=upper(c);
lc=length(c);
for i=1:lc
p(i)=int16(c(i))-65+sh;
end
p=mod(p,26)+97;
p=char(p);
disp( p)
end
end
输出示例:
Breaking Ceaser Cipher
Enter the message:
my name is jeff
Enter shift:
5
rdysfrjynxyojkk
在这里我们看到加密是正确的,但是空格已被'y'代替。当用作输入时,它不会替换字符“ y”,空格键以某种方式显示为“ y”。
我还尝试使用p2 = regexprep(c,'y',' ')
来用空格替换'y'字符串。还研究了isspace函数。没有运气
解决方法
您已经到达一半:
spaces=isspace(c)
% make array of spaces
out=blanks(size(c));
% get array without spaces
c=c(~spaces);
% do stuff to c,without spaces.
p=mod(p,26)+97;
p=char(p);
% Fill p in corresponding locations
out(~spaces)=p;