Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to find the length of the shortest word in a string with Pascal

1 Answer

0 votes
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

*)


 



answered 18 hours ago by avibootz
...