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

the cyclist
the cyclist 2020년 4월 21일
편집: the cyclist 2020년 4월 21일
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
AJ Schmidt 2020년 4월 21일
The rule is supposed to take the last letter of every word and then the number of characters in every word, so the password for your string is correct. I'm not sure how to combine the use of both characters and string values to get the password. The password can be returned as a string or character array as far as I know, so long as the correct password is found.
>> str = 'I like MATLAB'
>> regexprep(str,'\s*(\w*)(\w)','$2${num2str(1+numel($1))}')
ans =
'I1e4B6'

댓글을 달려면 로그인하십시오.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 21일

0 개 추천

Try these two options
function newpass = my_password1(inphrase)
words = strsplit(inphrase, ' ');
last_char = cellfun(@(x) {x(end)}, words);
lens = cellfun(@(x) {num2str(numel(x))}, words);
X = cell(1,numel(words));
X(1:2:end) = last_char(1:2:end);
X(2:2:end) = lens(2:2:end);
newpass = strjoin(X, '');
end
function newpass = my_password2(inphrase)
words = strsplit(inphrase, ' ');
last_char = cellfun(@(x) {x(end)}, words);
lens = cellfun(@(x) {num2str(numel(x))}, words);
newpass = strjoin(strcat(last_char, lens), '');
end
Test:
my_password1('A quick brown fox')
ans =
'A5n3'
my_password2('A quick brown fox')
ans =
'A1k5n5x3'

댓글 수: 6

AJ Schmidt
AJ Schmidt 2020년 4월 21일
Your second function finds the right answer, but we haven't learned the cellfun function yet, so I'm not sure I would be able to use that on a test or assignment, but thanks for finding a correct answer!
Ameer Hamza
Ameer Hamza 2020년 4월 21일
cellfun is just a compact way to write a for loop. It applies the specified function to each element of the cell array.
AJ Schmidt
AJ Schmidt 2020년 4월 21일
I have learned how to use for loops, so that could be a useful tool on my next exam. Would you mind putting for loops into your function instead of the cellfuns so I can compare the two?
Let me give one example, you can convert others in a similar way.
last_char = cellfun(@(x) {x(end)}, words);
is equivalent to
last_char = cell(1, numel(words));
for i=1:numel(words)
last_char{i} = words{i}(end);
end
AJ Schmidt
AJ Schmidt 2020년 4월 21일
Awesome, thanks for your help!
Ameer Hamza
Ameer Hamza 2020년 4월 21일
I am glad to be of help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 4월 21일

댓글:

2020년 4월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by