How to check if a char array contains only digits in Pascal

2 Answers

0 votes
program CheckDigitsOnly;

// PChar is a pointer that points to an array of type Char
function IsDigitArray(p: PChar): Boolean;
begin
    while p^ <> #0 do
    begin
        if (p^ < '0') or (p^ > '9') then
        begin
            IsDigitArray := False;
            Exit;
        end;
        Inc(p);
    end;
    IsDigitArray := True;
end;
 
var
    arr: array[0..5] of Char = '828190';
begin
    if IsDigitArray(@arr[0]) then
        WriteLn('yes')
    else
        WriteLn('no');
end.

 
    
(*
run:
    
yes
    
*)

 



answered Dec 30, 2024 by avibootz
edited Dec 30, 2024 by avibootz
0 votes
program CheckDigitsOnly;

uses SysUtils;

function IsDigitArray(arr: array of Char): Boolean;
var
  i: Integer;
begin
  IsDigitArray := True;
  for i := Low(arr) to High(arr) do
  begin
    if not (arr[i] in ['0'..'9']) then
    begin
      IsDigitArray := False;
      Break;
    end;
  end;
end;

var
  arr: array[0..5] of Char = ('8', '2', '8', '1', '9', '0');
  isDigits: Boolean;
begin
  //isDigits := IsDigitArray(arr);
  if IsDigitArray(arr) then
    WriteLn('yes')
  else
    WriteLn('no');
end.



   
(*
run:
   
yes
   
*)

 



answered Dec 30, 2024 by avibootz
...