How to check if a string is title case in Pascal

1 Answer

0 votes
program CheckTitleCase;

uses
  SysUtils;

function IsTitleCase(const S: string): Boolean;
var
  i: Integer;
  NewWord: Boolean;
  c: Char;
begin
  NewWord := True;

  for i := 1 to Length(S) do
  begin
    c := S[i];

    if c <= ' ' then  // whitespace check
      NewWord := True
    else
    begin
      if NewWord then
      begin
        if not (c in ['A'..'Z']) then
          Exit(False);
        NewWord := False;
      end
      else
      begin
        if not (c in ['a'..'z']) then
          Exit(False);
      end;
    end;
  end;

  IsTitleCase := True;
end;

begin
  Writeln(IsTitleCase('Hello World'));   // TRUE
  Writeln(IsTitleCase('Hello world'));   // FALSE
  Writeln(IsTitleCase('hello World'));   // FALSE
end.



(*
run:

TRUE
FALSE
FALSE

*)

 



answered 8 hours ago by avibootz
...