필터 지우기
필터 지우기

Find the longest word in a character vector

조회 수: 4 (최근 30일)
Danny
Danny 2012년 4월 7일
댓글: Walter Roberson 2018년 6월 16일
I have to write a function that consumes a character vector in the form of a sentence and returns the longest word to in the string to the user.
I think I am on the right track but I'm stuck.
This is what I have so far.
function word = longest_word(phrase)
code = double(phrase);
i_spaces = find(code == 32);
word = char(code(1:i_spaces(1)));
for i = 1:length(i_spaces)
if (i+1) > length(i_spaces)
break
elseif length(code(i_spaces(i)+ 1:i_spaces(i)))>length(word)
word = char(code(i_spaces(i)+ 1:i_spaces(i)));
end
end
end
i_spaces is the vector containing the locations of every space in between words.

답변 (3개)

Andrei Bobrov
Andrei Bobrov 2012년 4월 7일
phstr = 'etetetettete fhfhfh fg fgfgf tdfhfjfkfkfk nfjfiekehdfjgjgugtltrj'
wcl = regexp(phstr,'\w*','match')
[~,ii] = max(cellfun('length',wcl))
out = wcl(ii)

Walter Roberson
Walter Roberson 2012년 4월 7일
Words do not end only with spaces; they can end with any punctuation mark. Beware the apostrophe, which might mark the end of something being quoted, or might mark the possessive form, or might mark a contraction. Beware that commas and periods do not mark the end of a "word" when the word is a number. Beware that currency signs might come before or after a number, and if immediately adjacent to the number are sometimes considered to be part of that "word".
If you know the locations of all of words and have created a canonical form of changing all non-word characters to spaces and then all runs of spaces to single spaces, then think about using diff()
  댓글 수: 2
Danny
Danny 2012년 4월 7일
This question is for an introductory MATLAB course and we are only considering spaces as word separators for simplicity.
And I am only allowed to use commands I have learned in class. Unfortunately diff is not one of them.
Thank you for your answer.
Walter Roberson
Walter Roberson 2012년 4월 7일
diff(x) for a vector x, is x(2:end) - x(1:end-1), and you've learned those commands.

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


Humberto Lopez Franco
Humberto Lopez Franco 2018년 6월 16일
Does anyone care to optimize it for me???
clear;clc
fprintf(2,'I can tell you the longest word in your sentence!\n');
x=input('Go ahead, say something: ','s');
TF=isletter(x);
limit=size(x);
i=1;
while i<=limit(2)
if TF(i)~=1
x(i)=' ';
i=i+1;
else
i=i+1;
end
end
answer=strsplit(x);
i=1;
limit=size(answer);
y=zeros(1,limit(2));
while i<=limit(2)
y(i)=strlength(answer(i));
i=i+1;
end
i=1;
j=2;
while i<=limit(2) && j<=limit(2)
if y(i)>=y(j)
j=j+1;
else
i=j;
j=j+1;
end
end
A=string(answer(i));
fprintf('\n\nOk, so "%s" is the longest word in your sentence with a mere %i letter(s).\n\n',A,y(i))
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 6월 16일
No need to loop near the beginning.
x(~isletter(x)) = ' ';
Walter Roberson
Walter Roberson 2018년 6월 16일
arrayfun(@strlength, ...)

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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by