function Words()
% Looking for the longest word in a string.
n = 50;
letters = 'abcdef ';
s = [ ' ' RandString(n,letters) ' ']

% We will search the string s. When we detect a word, we will
% see if it is the longest word so far. maxLength will keep track of this
% statistic...
maxLength = 0;
% As we process a word, we will use this variable to tabulate its length..
count = 0;
% Note that s has length n+2. We look at each character and check what comes
% before.
for k=2:n+2
% s(k) is the "current character" and s(k-1) is the "previous
% character". There are 4 blank/nonblank possibilities ane they signal
% different things.
if isBlank(s(k))
% The current character is a blank
if isBlank(s(k-1))
% The previous character is a blank. Nothing to do because we
% are in between words!
else
% The previous character is not a blank. This
% means that a word has just ended so check to see if it is
% the longest word so far...
if count>maxLength
maxLength = count;
end
% Reset count...
%count = 0;
end
else
% The current character is not a blank
if ~isBlank(s(k-1))
% The previous character is not a blank so we are still inside
% a word.
count = count+1;
else
% The previous character is a blank
% We are starting a word...
count = 1;
end

end
end

maxLength = maxLength

function alfa = isBlank(c)
alfa = strcmp(c,' ');

function s = RandString(n,letters)
% letters is a string consisting of m distinct characters.
% s is a length n string whose characters are randomly selected from
% letters.
s = blanks(n);
m = length(letters);
for k=1:n
c = letters(ceil(rand(1)*m));
s(k) = c;
end