How to change multiple line text into vector (string)

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 件のコメント

Adam Danz
Adam Danz 2019 年 11 月 21 日
The first block of your code seems to function well with a test .txt file of mine.
I haven't looked into the 2nd block of your code. I suggest you execute the code in debug mode and check out each line, line-by-line, to investigate where the unexpected outcome happens.
Piotr Piwowarski
Piotr Piwowarski 2019 年 11 月 22 日
Ok, so I edited question.
Program displays multiple line of text, I know. But when I want to do something with it it doesnt read it in a way I want him to.
I want program to read line by line or to remove every "enter" from a text to create one line text.
Walter Roberson
Walter Roberson 2019 年 11 月 22 日
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.
Piotr Piwowarski
Piotr Piwowarski 2019 年 11 月 22 日
Yeah, changed as Stephen said and it works!

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2019 年 11 月 22 日
編集済み: Stephen23 2019 年 11 月 22 日

1 投票

"..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);

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEncryption / Cryptography についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by