Shifting letters by two positions. What's wrong with the code?
Mostrar comentarios más antiguos
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 comentario
Simpler, and less liable to mistakes:
alphab = 'a':'z'
This line does nothing because its output is ignored:
a = find(inStr(i)==alphab)
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 11 de Feb. de 2018
0 votos
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.
Categorías
Más información sobre Common Operations en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!