Using masking and native string functions to find the longest word in a string.

7 views (last 30 days)
Hi all,
I was wondering if someone would be able to help me with the logic for this problem: given an input string, use a function to return the length and a string of the longest word in the input string.
*Note: I cannot use conditionals or loops for this problem. Words are only divided by spaces and there are no duplicate length longest words.
My first instict was to use strfind to determine the indices of the spaces, and find the largest range between these indices, giving me both the length and a way to extract the longest word from the index. I'm a little stuck on how to determine the largest ranges without using conditionals though.
Alternatively, I've considered using masking to determine where the characters are in the input string but again, I'm not sure where to continue from here.
Any and all help is appreciated. Thank you!
At the moment, my code looks like this:
function [out, length] = longestWord(in)
%find all the spaces
%find biggest range between spaces -> also the length
%mask for that range and extract word
spaces = strfind(in, ' ');
numWords = length(spaces);
range1 = spaces(1);
range2 = spaces(2-1);
rangeN = ... %this is where I'm having my issue becuase I can't figure out a way to stop finding ranges w/o a conditional.
end
An example input/output is:
[word3, len3] = longestWord('Today is a good day')
>> word3 = 'Today'
>> len3 = 5
  3 Comments
Conolly
Conolly on 2 Feb 2023
Edited: Conolly on 2 Feb 2023
@Stephen23 thank you very much for your help. I took the logic you used and figured out a solution using what I've been taught (below if you're curious). I tried to use arrays as much as possible but it can certainly be optimized as you mentioned.
function [out, lengthWord] = longestWord(in)
a = length(in);
spaces = strfind(in,' ');
wordStart = [1 spaces];
wordEnd = [spaces-1 a];
wordSizes = wordEnd - wordStart;
wordSizes(1) = wordSizes(1) +1;
[lengthWord,index] = max(wordSizes);
out = in(wordStart(index):wordEnd(index));
a = strfind(out, ' ');
out(a) = [];
end
Stephen23
Stephen23 on 2 Feb 2023
If you specify the WORDSTART/END properly then you do not need a special-case for the first word and you do not need that STRFIND() at the end:
T = 'Today is a good day';
N = numel(T);
X = strfind(T,' ');
wordStart = [1,1+X];
wordEnd = [X-1,N];
wordSizes = 1 + wordEnd - wordStart
wordSizes = 1×5
5 2 1 4 3
[lengthWord,Y] = max(wordSizes);
out = T(wordStart(Y):wordEnd(Y))
out = 'Today'

Sign in to comment.

Answers (1)

KSSV
KSSV on 2 Feb 2023
str = 'Today is a good day' ;
s = strsplit(str) ;
L = cellfun(@length,s) ;
[val,idx] = max(L) ;
longest_string = s{idx} ;
fprintf('Longest string in given string is: %s, its length is %d\n',longest_string,val)
Longest string in given string is: Today, its length is 5
  1 Comment
Conolly
Conolly on 2 Feb 2023
Edited: Conolly on 2 Feb 2023
Thank you so much! Just out of curiosity, is there also a relatively efficient way to solve this using masking. Your answer definitely solves my issue but I would love to increase my understanding if I can.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!