How to check whether each substring range (L to R) from an array of substring ranges is a palindrome in Pascal

1 Answer

0 votes
program PalindromeCheck;

const
  Rows = 3;

type
  RangeArray = array[1..Rows, 1..2] of Integer;

function IsPalindrome(const S: string; Left, Right: Integer): Boolean;
begin
  while Left < Right do
  begin
    if S[Left] <> S[Right] then
    begin
      IsPalindrome := False;
      Exit;
    end;
    Inc(Left);
    Dec(Right);
  end;
  IsPalindrome := True;
end;

procedure CheckRangesForPalindrome(const S: string; const Ranges: RangeArray);
var
  I, StartIdx, EndIdx: Integer;
  SubStr: string;
begin
  for I := 1 to Rows do
  begin
    StartIdx := Ranges[I, 1] + 1;  
    EndIdx := Ranges[I, 2] + 1;
    SubStr := Copy(S, StartIdx, EndIdx - StartIdx + 1);

    Write(SubStr, ': ');
    if IsPalindrome(S, StartIdx, EndIdx) then
      WriteLn('Palindrome')
    else
      WriteLn('Not palindrome');
  end;
end;

var
  Str: string;
  Ranges: RangeArray;

begin
  Str := 'abcddcbeebc';
  Ranges[1, 1] := 2; Ranges[1, 2] := 5;
  Ranges[2, 1] := 5; Ranges[2, 2] := 10;
  Ranges[3, 1] := 3; Ranges[3, 2] := 7;

  CheckRangesForPalindrome(Str, Ranges);
end.



(*
run:

cddc: Palindrome
cbeebc: Palindrome
ddcbe: Not palindrome

*)

 



answered 12 hours ago by avibootz

Related questions

...