Matlab 字符显示为';y';而不是一个空格';
在matlab中,我正在编写一个Ceaser密码,但空格显示为“y”字符。 我怎样才能用空格来代替它Matlab 字符显示为';y';而不是一个空格';,matlab,caesar-cipher,Matlab,Caesar Cipher,在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');
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;
您能用
isspace
向我们展示您的尝试吗?我不明白为什么它不起作用,我添加了c=c(find(~isspace(c));就在“sh”变量下面。当使用相同的明文和移位号时,我得到的输出是:rdsfrjnxojkkRight。所以你或多或少都拥有它。您已经选择了所有非空格的c
,这些空格工作正常。你只是忘了稍后再添加空格!注意,您不需要查找,逻辑索引可以工作:c=c(~isspace(c))代码>我想我的问题是,如何以及在哪里重新添加空间?