MATLAB: Made a text encryption program in school, it works for short words but when I fill in complete sentences (or really long words) something goes wrong

bamiencryptionMATLABsomethingwrong

So this is the code, each letter will make an array entry of 9 digits, say, I have a word of 5 letters it will make an array with 5 entries that all contain 9 digits, then I make a string of all the digits and if I divide the length of the string by 9, it should give an integer. For short words it actually does, but when I enter longer words something goes wrong. Can someone help me?
clc
a = 815; A = 534;
b = 906; B = 695;
c = 127; C = 317;
d = 913; D = 950;
e = 632; E = 439;
f = 198; F = 382;
g = 278; G = 766;
h = 547; H = 795;
i = 958; I = 187;
j = 965; J = 490;
k = 158; K = 446;
l = 971; L = 646;
m = 485; M = 709;
n = 800; N = 755;
o = 142; O = 276;
p = 422; P = 680;
q = 916; Q = 655;
r = 792; R = 163;
s = 656; S = 119;
t = 849; T = 498;
u = 758; U = 340;
v = 392; V = 585;
w = 171; W = 224;
x = 706; X = 751;
y = 277; Y = 255;
z = 823; Z = 506;
space = 109;
dot = 254;
comma = 736;
exlm = 825;
quest = 235;
at = 199;
dblpnt = 501;
prompt = 'Enter text to encrypt: ';
INPUT = input(prompt,'s');
END=size(INPUT,2);
ARRAY=zeros(1,END);
ARRAY2=zeros(1,END);
for II = 1:END
if INPUT(II) == ' '
LET=space;
elseif INPUT(II) == ','
LET=comma;
elseif INPUT(II) == '.'
LET=dot;
elseif INPUT(II) == '@'
LET=at;
elseif INPUT(II) == ':'
LET=dblpnt;
elseif INPUT(II) == '!'
LET=exlm;
elseif INPUT(II) == '?'
LET=quest;
else
LET=eval(INPUT(II));
end
ARRAY(1,II)=LET;
end
for JJ = 1:END
ARRAY(1,JJ)=ARRAY(1,JJ)+100000000+261785000+JJ*12*23;
end
for KK = 1:END
STR=num2str(ARRAY(1,KK));
if mod(KK,2)==0
EVENSTR=STR([6 2 3 8 1 9 5 4 7]);
ARRAY2(1,KK)=str2num(EVENSTR);
else
ODDSTR=STR([3 5 4 8 1 7 9 6 2]);
ARRAY2(1,KK)=str2num(ODDSTR);
end
end
Encrypted_text='';
FARRAY=flip(ARRAY2);
CIRCD=circshift(FARRAY,[1 floor(END/2)]);
for LL = 1:END
Encrypted_text=([Encrypted_text,num2str(CIRCD(LL))]);
end
clc
fprintf('Your encrypted text:%s\n')
fprintf(2,' %s\n', Encrypted_text)
clear all

Best Answer

Thomas - your code (which is neat!) does a number of conversions from numbers to strings and back again to numbers. As you mention in your question, each input letter (to be encrypted) makes up an array entry of nine digits. The code then does some re-ordering of the digits (among other things), and this re-ordering seems to be occurring here
for KK = 1:END
STR=num2str(ARRAY(1,KK));
if mod(KK,2)==0
EVENSTR=STR([6 2 3 8 1 9 5 4 7]);
ARRAY2(1,KK)=str2num(EVENSTR);
else
ODDSTR=STR([3 5 4 8 1 7 9 6 2]);
ARRAY2(1,KK)=str2num(ODDSTR);
end
end
Note how the re-ordering uses the string equivalent of the number, and then str2num is used to create the numeric entry. Now what happens if the new order of the digits is such that a zero is moved to the "front" (left most digit), and we get something like
ODDSTR = '012345678';
When we convert this to a number, we lose the zero
str2num(ODDSTR)
ans =
12345678
and now our number which was originally nine digits long is now eight. So you will need to pad these numbers before creating the encrypted text
for LL = 1:END
numAsText = num2str(CIRCD(LL));
lenText = length(numAsText);
if lenText<9
% length of string is less than nine so prefix with zeros
numAsText = [repmat('0',1,9-lenText) numAsText];
end
Encrypted_text=([Encrypted_text, numAsText]);
end