Function in Separate File
Show older comments
Hi all. I need to convert this encryption program into 2 separate functions, one for encryption, and one for decryption. I am confused at the moment. The code below is the current code with everything in one file. How would I create 2 separate functions for the encryption and decryption? Thanks
clear
clc
%Prompts for user input
message = input('Enter a message to encode: ', 's');
encryption = message;
%Sets the terms and conditions for encoding characters, and if else then
%mapping them to themselves
for i = 1:length(encryption)
x = encryption(i);
if x >= 'A' && x <= 'Z'
if x >= 'U'
x = x - 20;
else
x = x + 6;
end
elseif x>= 'a' && x <= 'z'
if x >= 'u'
x = x - 20;
else
x = x + 6;
end
end
encryption(i) = x;
end
%Same thing as above except dealing with the decryption, mapping to
%themselves if necessary.
decryption = encryption;
for i = 1:length(decryption)
x = decryption(i);
if x >= 'A' && x <= 'Z'
if x <= 'F'
x = x + 20;
else
x = x - 6;
end
elseif x>= 'a' && x <= 'z'
if x <= 'f'
x = x + 20;
else
x = x - 6;
end
end
decryption(i) = x;
end
%Prints the output
fprintf("\nMessage: %s\n\n", message);
fprintf("Encrypted: %s\n\n", encryption);
fprintf("Decrypted: %s\n\n", decryption);
Accepted Answer
More Answers (0)
Categories
Find more on Desktop in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!