How could I fix my script?

19 vues (au cours des 30 derniers jours)
michael story
michael story le 8 Oct 2018
If I am using the function rotatedText=rot(text,n). First things for say, I have to make sure that my script should apply a Caesar cipher encryption with a shift to n to input string text. The function should shift uppercase letters to uppercase letters and lowercase letters to lowercase letters. If the string text contains numbers or special characters it should leave them the same. The function should work for negative shifts and shifts greater than 26 or less than -26.
function rotatedText=rot(text,n);
n=mod(n,26)
for i=1:length(text)
ascii=double(txt(i));
if ascii>=65 && ascii<90
rotatedText(i)=char(ascii+n);
elseif ascii>=97 && ascii<=122
rotatedText(i)=char(ascii+n)
else ascii>=33 && ascil<=65 || ascii>=90 && ascii<97 || ascii>=122 &&
ascii<=126;
rotatedText(i)=char(ascii);
end
end
  3 commentaires
Stephen23
Stephen23 le 8 Oct 2018
@michael story: and what is the expected output?
michael story
michael story le 8 Oct 2018
'dEbc'

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Oct 2018
Decide which range you are in. If it is one of the two ranges to be affected, record the character that is the beginning of the range and calculate the distance of the character from the beginning of the range. Add the shift to the distance. Take mod 26. Add the recorded beginning of the range, and convert to char and save. Otherwise save unchanged. Repeat for all characters.
... You can vectorize this in at most two cases.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by