How to cipher an input file?
Show older comments
Hi,
I'm woring on a program that ciphers files (Caesar cipher). The program works when an user inputs a sentence, but protests when somebody is trying to do the same with a file, any idea what am I doing wrong?
clear all
fid = input('Podaj tekst:','s')
if exist(fid, 'file')%File exists
Alfabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o','p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W','V', 'X', 'Y', 'Z' ];
i = 1;
key = input('Podaj skok:'); %Jump
plik = dlmread(fid)% Tried plik = fopen(fid) and plik = importdata(fid) as well, but those don't work either.
while key > 26
key = mod(key,26);
end
while i <= length(plik);
if isletter(plik(i)) == 1
y = strfind(Alfabet,plik(i));
z = y+key;
if z > 26
z = z - 26;
end
Szyfr(i) = Alfabet(z);
elseif isletter(plik(i)) == 0
if plik(i) == ' '
Szyfr(i)=plik(i);
end
if plik(i) == '.'
Szyfr(i) = plik(i);
end
if plik(i) == ','
Szyfr(i) = plik(i);
end
if plik(i) == ':'
Szyfr(i) = plik(i);
end
end
i = i+1;
end
dlmwrite('Cezar.txt',Szyfr,'\n')
dlmwrite('Skok.txt',key,'\n')
else
fprintf('Podany plik nie istnieje:\n%s', plik); %File doesn't exist
end
Accepted Answer
More Answers (0)
Categories
Find more on Programming 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!