Keeping Previous Iteration changes

I am trying to change the user inputted text with the apropriate index of cipher vector. However, i cant seem to be able to keep the changes from the previous iterations of the for loop. How can i keep the changes with each iteration?
cipherVector = ['D','R','I','U','S','Y','A','Q','B','W','T','E','Z','N','L','P','M','O','C','V','H','G','F','J','X','K'];
letterVector = ['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'];
userEncrypt = input('Enter sentence','s')
for i= 1: strlength(userEncrypt)
if ismember(lower(userEncrypt(i)), letterVector)
x = strrep(userEncrypt, userEncrypt(i), cipherVector(strfind(letterVector,lower(userEncrypt(i)))))
end
end

4 comentarios

Fabio Freschi
Fabio Freschi el 31 de Oct. de 2019
Why did you delete your question? It could be of interest for Matlab community
Adam Danz
Adam Danz el 31 de Oct. de 2019
@Nelson Gonzalez, please revert your question. It's disrespectful to remove your question and make it useless after volunteers gave up their time to help you.
@Fabio Freschi, if you approximately remember the question I'd be glad to edit the question and replace the text if you don't have those privileges yet.
Rena Berman
Rena Berman el 12 de Dic. de 2019
(Answers Dev) Restored edit
Adam Danz
Adam Danz el 12 de Dic. de 2019
Thank you Rena Berman

Iniciar sesión para comentar.

Respuestas (1)

Fabio Freschi
Fabio Freschi el 27 de Oct. de 2019
I see two problems in your code
1) the changes are only temporarily saved in x and x is never used
2) onec you change a letter, all the characters with that letter are changed, than the cypherLetter is changed again. For example if your sentence is 'aaa', after the first iteration you get 'DDD' and the second D is then processed at the second iteration.
I changed your code like this
cipherVector = ['D','R','I','U','S','Y','A','Q','B','W','T','E','Z','N','L','P','M','O','C','V','H','G','F','J','X','K'];
letterVector = ['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'];
userEncrypt = input('Enter sentence: ','s');
for i= 1: strlength(userEncrypt)
% find position
iPos = find(letterVector==lower(userEncrypt(i)));
if ~isempty(iPos)
userEncrypt(i) = cipherVector(iPos);
end
end
disp(userEncrypt)
I hope this meets your initial request

Categorías

Más información sobre Encryption / Cryptography en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Oct. de 2019

Comentada:

el 12 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by