I can't choose just names!
1 view (last 30 days)
Show older comments
Depressive Mood
on 14 May 2020
Commented: Image Analyst
on 14 May 2020
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
Accepted Answer
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
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
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.
More Answers (0)
See Also
Categories
Find more on Octave in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!