Combining string and numerical values
Mostrar comentarios más antiguos
Hi, I'm doing a review sheet (not for a grade), and I'm hitting a snag with combining string and character vectors. Here's my problem:
Write a function my_password that will receive a string or character vector with a message and create a password that alternates between the final letter of each word and length of each word.
I've tried the following two functions, the first one only works if your in phrase is one word, and the second returns only a character vector:
function password=my_password(inphrase)
password= inphrase(end);
passnum= strlength(inphrase);
newpass= strcat(password, int2str(passnum))
end
function password = mypassword(inphrase);
rest = strtrim(char(inphrase));
password = '';
while ~isempty(rest)
[word, rest] = strtok(rest);
password = strcat(password,int2str(word));
end
end
3 comentarios
the cyclist
el 21 de Abr. de 2020
Editada: the cyclist
el 21 de Abr. de 2020
I'm not certain I understand the rule for password creation. Is this right?
inphrase = 'I like MATLAB'
leads to
password = 'I1e4B6'
Also, can the password be returned as either string or character array, or does it need to be the same type as the input?
AJ Schmidt
el 21 de Abr. de 2020
Stephen23
el 21 de Abr. de 2020
>> str = 'I like MATLAB'
>> regexprep(str,'\s*(\w*)(\w)','$2${num2str(1+numel($1))}')
ans =
'I1e4B6'
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!