How to count words in a string with Pascal

1 Answer

0 votes
program CountWords;

var
  str: string;
  i, wordCount: Integer;
  inWord: Boolean;

begin
  str := 'Pascal is an imperative and procedural programming language';
  wordCount := 0;
  inWord := False;

  for i := 1 to Length(str) do
  begin
    if str[i] <> ' ' then
    begin
      if not inWord then
      begin
        inWord := True;
        Inc(wordCount);
      end;
    end
    else
      inWord := False;
  end;

  WriteLn('Word count: ', wordCount);
end.




(*
run:

Word count: 8

*)

 



answered Nov 2, 2025 by avibootz
...