- #1
G'mo
- 3
- 0
I have the following ciphering program.
a1 = input('Please enter a sentence: ','s');
x = upper(a1); % Change the text to uppercase
p = CleanStr(x) % Call a function CleanStr to remove
% all unwanted characters
double(p); % String to ASCII numbers
for i = 1:numel(p) % Iterate as long as there are characters
% in the string
if (p(i) == 65) % Find all ASCII codes which are less
% than 88 and not spaces
p(i) = 83; % Replace each letter with the letter
% 3 places along in the alphabet
elseif (p(i) == 66)
p(i) = 81;
elseif p(i) == 67;
p(i) = 85;
elseif p(i) == 68
p(i) = 89;
elseif (p(i) == 69) | (p(i) == 89)
p(i) = 78;
elseif (p(i) == 70) | (p(i) == 75)
p(i) = 87
elseif p(i) == 71
p(i) = 69;
elseif (p(i) == 73 ) | (p(i) == 81) | (p(i) == 83)
p(i) = 72;
elseif p(i) == 74
p(i) = 71;
elseif p(i) == 76
p(i) = 84;
elseif p(i) == 77
p(i) = 68;
elseif p(i) == 78
p(i) = 65;
elseif p(i) == 79
p(i) = 88;
elseif p(i) == 80
p(i) = 70;
elseif p(i) == 84
p(i) = 77;
elseif p(i) == 85
p(i) = 66;
elseif p(i) == 86
p(i) = 89;
elseif p(i) == 87
p(i) = 80;
elseif p(i) == 88
p(i) = 76;
elseif p(i) == 90
p(i) = 73;
else
p(i) = 32;
end
end
disp(['The enciphired message is: ',num2str(p)]);
Is there a way of making this program any shorter? ( Maybe vectorising). The program ciphers as follows: (ABCDEFGHIJKLMNOPQRSTUVWXYZ ----> SQUYNWEZHGWTDAXFHRHMBZPLNI).
Thank you for your help guys.
a1 = input('Please enter a sentence: ','s');
x = upper(a1); % Change the text to uppercase
p = CleanStr(x) % Call a function CleanStr to remove
% all unwanted characters
double(p); % String to ASCII numbers
for i = 1:numel(p) % Iterate as long as there are characters
% in the string
if (p(i) == 65) % Find all ASCII codes which are less
% than 88 and not spaces
p(i) = 83; % Replace each letter with the letter
% 3 places along in the alphabet
elseif (p(i) == 66)
p(i) = 81;
elseif p(i) == 67;
p(i) = 85;
elseif p(i) == 68
p(i) = 89;
elseif (p(i) == 69) | (p(i) == 89)
p(i) = 78;
elseif (p(i) == 70) | (p(i) == 75)
p(i) = 87
elseif p(i) == 71
p(i) = 69;
elseif (p(i) == 73 ) | (p(i) == 81) | (p(i) == 83)
p(i) = 72;
elseif p(i) == 74
p(i) = 71;
elseif p(i) == 76
p(i) = 84;
elseif p(i) == 77
p(i) = 68;
elseif p(i) == 78
p(i) = 65;
elseif p(i) == 79
p(i) = 88;
elseif p(i) == 80
p(i) = 70;
elseif p(i) == 84
p(i) = 77;
elseif p(i) == 85
p(i) = 66;
elseif p(i) == 86
p(i) = 89;
elseif p(i) == 87
p(i) = 80;
elseif p(i) == 88
p(i) = 76;
elseif p(i) == 90
p(i) = 73;
else
p(i) = 32;
end
end
disp(['The enciphired message is: ',num2str(p)]);
Is there a way of making this program any shorter? ( Maybe vectorising). The program ciphers as follows: (ABCDEFGHIJKLMNOPQRSTUVWXYZ ----> SQUYNWEZHGWTDAXFHRHMBZPLNI).
Thank you for your help guys.