program ShortestWordLength;
function ShortestWordLen(s: string): integer;
var
i, currentLen, minLen: integer;
begin
minLen := 255; { start with a very large number }
currentLen := 0;
for i := 1 to length(s) do
begin
if s[i] <> ' ' then
inc(currentLen)
else
begin
if currentLen > 0 then
if currentLen < minLen then
minLen := currentLen;
currentLen := 0;
end;
end;
{ Handle last word if string doesn't end with a space }
if currentLen > 0 then
if currentLen < minLen then
minLen := currentLen;
{ If no words found, return 0 }
if minLen = 255 then
ShortestWordLen := 0
else
ShortestWordLen := minLen;
end;
var
input: string;
resultLen: integer;
begin
input := 'Find the shortest word length in this string';
resultLen := ShortestWordLen(input);
if resultLen = 0 then
writeln('No words found.')
else
writeln('Length of the shortest word: ', resultLen);
end.
(*
run:
Length of the shortest word: 2
*)