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

조회 수: 15 (최근 30일)
Conolly
Conolly 2023년 2월 2일
댓글: Stephen23 2023년 2월 2일
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
Conolly
Conolly 2023년 2월 2일
편집: Conolly 2023년 2월 2일
@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 2023년 2월 2일
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'

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

답변 (1개)

KSSV
KSSV 2023년 2월 2일
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
Conolly
Conolly 2023년 2월 2일
편집: Conolly 2023년 2월 2일
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.

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

카테고리

Help CenterFile Exchange에서 Signal Attributes and Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by