Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to check if a string is a palindrome ignoring case and non-alphanumeric characters in Pascal

1 Answer

0 votes
program PalindromeChecker;

function IsAlphaNumeric(ch: Char): Boolean;
begin
  IsAlphaNumeric := ((ch >= 'A') and (ch <= 'Z')) or
                    ((ch >= 'a') and (ch <= 'z')) or
                    ((ch >= '0') and (ch <= '9'));
end;

function ToLower(ch: Char): Char;
begin
  if (ch >= 'A') and (ch <= 'Z') then
    ToLower := Chr(Ord(ch) + 32)
  else
    ToLower := ch;
end;

function IsPalindrome(str: string): Boolean;
var
  filtered: string;
  i, len: Integer;
begin
  filtered := '';
  
  { Filter out non-alphanumeric characters and convert to lowercase }
  for i := 1 to Length(str) do
  begin
    if IsAlphaNumeric(str[i]) then
      filtered := filtered + ToLower(str[i]);
  end;

  Writeln(filtered);

  len := Length(filtered);
  IsPalindrome := True;
  for i := 1 to len div 2 do
  begin
    if filtered[i] <> filtered[len - i + 1] then
    begin
      IsPalindrome := False;
      Exit;
    end;
  end;
end;

var
  input: string;
begin
  input := '+^-Ab#c!D 50...#  05*()dcB[]A##@!$';

  if IsPalindrome(input) then
    Writeln('The string is a palindrome.')
  else
    Writeln('The string is not a palindrome.');
end.




(*
run:
  
abcd5005dcba
The string is a palindrome.

*)



 



answered Aug 9, 2025 by avibootz
...