How to check if string is palindrome in Pascal

1 Answer

0 votes
program CheckPalindrome;

function IsPalindrome(s: string): Boolean;
var
  i, len: Integer;
begin
  len := Length(s);
  for i := 1 to len div 2 do
  begin
    if s[i] <> s[len - i + 1] then
    begin
      IsPalindrome := False;
      Exit;
    end;
  end;
  IsPalindrome := True;
end;

var
  str: string;
begin
  str := 'abcba';
  str := LowerCase(str); // Optional: to make the check case-insensitive
  
  if IsPalindrome(str) then
    WriteLn('The string is a palindrome.')
  else
    WriteLn('The string is not a palindrome.');
end.



(*
run:

The string is a palindrome.

*)

 



answered Apr 25, 2025 by avibootz
...