How do I circularly shift the alphabet without using circshift function?

5 visualizzazioni (ultimi 30 giorni)
I'm trying to write a function that inputs the alphabet then shifts it by 3 so that then the original would be ABC...XYZ and then with the shift it'd look like XYZ...UVW.
Thanks for the help!
___________
Ok I typed this into matlab and it worked. I tried it again for a message that I want to shift 12 letters over, but it won't work.
lets say for example my message was: I HATE MATLAB.
How would i do a function so that the spaces and the punctuation would stay the same, but the message would then read: U TMFQ YMFXMN.
thanks again!

Risposte (3)

Anand
Anand il 21 Mar 2013
alphabet = 97 : 122;
>> char(alphabet)
ans =
abcdefghijklmnopqrstuvwxyz
shifted_alphabet = alphabet-3;
shifted_alphabet(1:3) = alphabet(end-2:end);
>> char(shifted_alphabet)
ans =
xyzabcdefghijklmnopqrstuvw

Azzi Abdelmalek
Azzi Abdelmalek il 21 Mar 2013
s='A':'Z';
s=[s(end-2:end) s(1:end-3)]

Azzi Abdelmalek
Azzi Abdelmalek il 22 Mar 2013
Modificato: Azzi Abdelmalek il 22 Mar 2013
shiftn=12
s='A':'Z';
str='I HATE MATLAB'
idx=regexp(str,'[A-Z]')
for k=idx
a=strfind(s,str(k))
str(k)=s(mod(a+shiftn,26))
end
%or
shiftn=12
str='I HATE MATLAB'
sth=double(str)-64
idx=regexp(str,'[A-Z]')
sth(idx)=mod(sth(idx)+shiftn,26)
out=char(sth+64)

Categorie

Scopri di più su MATLAB Compiler in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by