I can't choose just names!

1 次查看(过去 30 天)
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))

采纳的回答

Geoff Hayes
Geoff Hayes 2020-5-14
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 个评论
Geoff Hayes
Geoff Hayes 2020-5-14
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 2020-5-14
There may be some differences between MATLAB and Octave. I think you should ask in an Octave forum, or better yet, use MATLAB instead.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Octave 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by