MATLAB Answers

I can't choose just names!

2 views (last 30 days)
I have this question homework. And I can't find the result.And this is the question and the code which I wrote.
Assume that at each row of a 2D character array, a name exists between two asterisk signs (*). At each row, along with other characters, there is only one name and there are only two asterisk signs. A name can be anywhere in the row. Using Octave, write a function called "longest_name" that will accept an array with above-given properties as input and return the longest name in this array. Example: If Records = ['123*Ali*36' ; '*Veli*1783' ; '99*Zeynep*'] and Out = longest_name(Records), Out will be 'Zeynep'.
a = {'123*Ali*36' ; '*Veli*178' ; '99*Zeynep*'} ;
for k =1:length(a)
val(k)=size(a,1) ;
strfind=('*')
substr=(0, *)
end
out=a(val==max(val))

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 14 May 2020
Depressive Mood - since this his homework, we can only give out hints. You need to extract the name from between the two asterisks. You can do this with regular expressions but an easier way might be to use strsplit. How might using this function be helpful?

  3 Comments

Depressive Mood
Depressive Mood on 14 May 2020
Thanks for your answer.I shouldn't use strsplit because I haven't learn them at school. I know I have a mistake about using strfind and substr.I choose asterisks by using strfind,and seperate them by using substr.I defined them but code still doesn't work :(
Geoff Hayes
Geoff Hayes on 14 May 2020
You would need to use strfind as
indices = strfind(s, '*');
where s is your string that you want to find the indices of the asterisks. If indices is non-empty and has exactly two elements, then you know that everything between those two indices corresponds to the name.
a = {'123*Ali*36' ; '*Veli*178' ; '99*Zeynep*'} ;
for k =1:length(a)
s = a{k};
indices = strfind(s, '*');
% etc.
end
Image Analyst
Image Analyst on 14 May 2020
There may be some differences between MATLAB and Octave. I think you should ask in an Octave forum, or better yet, use MATLAB instead.

Sign in to comment.

More Answers (0)