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 find longest palindrome substring in Pascal

1 Answer

0 votes
program LongestPalindrome;

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

function LongestPalindromeSubstring(const s: string): Integer;
var
  i, j, k, len, start, longestLength: Integer;
begin
  len := Length(s);
  longestLength := 1;
  start := 1;

  for i := 1 to len do
    for j := i to len do
      if IsPalindrome(s, i, j) and ((j - i + 1) > longestLength) then
      begin
        start := i;
        longestLength := j - i + 1;
      end;

  Write('Longest palindrome substring = ');
  for k := start to start + longestLength - 1 do
    Write(s[k]);
  Writeln;

  LongestPalindromeSubstring := longestLength;
end;

var
  input: string;
  lengthFound: Integer;
begin
  input := 'qabcbaproggorpxyyxzv';
  
  lengthFound := LongestPalindromeSubstring(input);
  Writeln('Length palindrome substring length = ', lengthFound);
end.




(*
run:

Longest palindrome substring = proggorp
Length palindrome substring length = 8

*)



answered Nov 7, 2025 by avibootz
...