How to change multiple line text into vector (string)

8 views (last 30 days)
Hi, I have an annusual problem:
I have a task to creat program that will encrypt or decrypt a text from a .txt file in 3 methods: Ceasars, Vigeneres and Hills. When I had no real problem with encryption and decryption methods, I found out that my program works only on a text that has only one line of text. It doesnt work at all for a text with multiple lines, because it reads text in a way I dont want it to read.
Example:
Text:
Well, good night. If you happen to see Horatio
and Marcellus, who are supposed to stand guard with
me tonight, tell them to hurry.
Way program reads it:
Wameneld l t,Mo ...
Way I want program to reads it:
Well, good night, If [...] Horatio and Marcellus, who are [...]with me tonight
Can you show me how to remove "'enters" from a text or make program to read text line by line?
This is my function that reads a text from a .txt file:
D=input('name of a file (xyz.txt): ');
z=importdata(D);
E=char(z);
disp(E)
y=size(E,2); %this line is very usefull in encryption functions for me, because it specifies number of times that loops should work
Part of Ceasars cipher to show how my encryption functions work:
A=['A':'Z'];
B=['a':'z'];
C=['*'];
k=input('insert a key: ');
k=mod(k,26);
for i=1:y
if isletter(E(i)) == 1
if ismember(E(i),A) == 1 %loops for BIG letters
e=strfind(A,E(i));
if f == 1 %f=1 is for encryption, f=2 is for decryption: some switch functions going on
r=e+k;
if r>26
r=r-26;
end
elseif f== 2
r=e-k;
if r<=0
r=26+r;
end
end
out(i)=A(r);
%simmilar functions for small letters, and some basic ifs for characters like , . ? etc.
Other question: when I have my ecrypted/decrypted text by MatLab as an:
ans =
'Uhhgcccbbbccxllk gqqlrp y'
how to save this encrypted/decrypted text as an .txt file?
  4 Comments
Walter Roberson
Walter Roberson on 22 Nov 2019
If you want consistent sane reading of data, get rid of your call to importdata. Stephen's recommendation to use fileread() is a good one.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 22 Nov 2019
Edited: Stephen23 on 22 Nov 2019
"..or to remove every "enter" from a text to create one line text."
That is easy with fileread and regexprep:
>> str = fileread('test.txt')
str = Well, good night. If you happen to see Horatio
and Marcellus, who are supposed to stand guard with
me tonight, tell them to hurry.
>> str = regexprep(str,'\s+',' ')
str = Well, good night. If you happen to see Horatio and Marcellus, who are supposed to stand guard with me tonight, tell them to hurry.
"how to save this encrypted/decrypted text as an .txt file?"
Where str is the character vector to be printed to file:
[fid,msg] = fopen('output.txt','wt');
assert(fid>=3,msg)
fprintf(fid,'%s',str);
fclose(fid);

More Answers (0)

Categories

Find more on Encryption / Cryptography in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!