Shifting letters by two positions. What's wrong with the code?
Afficher commentaires plus anciens
I am trying to shift each letter of a given word by two positions.
alphab='abcdefghijklmnopqrstwxyz'
inStr = 'doug';
rot=2
b=''
for i=1:length(inStr)
a = find(inStr(i)==alphab)
a = (inStr(i)+rot)
if abs(a)>26
a = rem(a,26);
end
A = alphab(a)
b = strcat(b,A);
end
1 commentaire
Simpler, and less liable to mistakes:
alphab = 'a':'z'
This line does nothing because its output is ignored:
a = find(inStr(i)==alphab)
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 11 Fév 2018
0 votes
Hint: you can use the second output of ismember to give the index of the individual characters in the alphab .
Hint: a useful way to calculate wraparound is 1 + mod(value-1,base) . 26-1, mod 26 is 25, 25 + 1 is 26. 27-1, mod 26, is 0, 0 + 1 is 1.
Catégories
En savoir plus sur Common Operations dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!