Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find the longest palindrom in the word ?
06-13-2009, 02:39 AM
Post: #1
Find the longest palindrom in the word ?
This is competitive assignment, and it is about finding the longest palindrome in the word.
Palindrome is a word that you read the same from both sides.
Example :
radar

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
06-15-2009, 03:25 AM
Post: #2
RE: Find the longest palindrom in the word ?
This is complex issue if you want to make it right !
It is easy to make it to check palindrome for words, but for
whole sentences is a bit harder, but if you add the fact that
in the sentence could be lower case and upper case characters,
then you get a complex assignment !
Basically there are 3 functions :
Code:
lower_case_string(string)
del_separations(string)
palindrome_check(string)
First function makes whole string lower case !
It checks if string is upper case and then converts it to lowercase !
Second function deletes separations from string (if string is sentence and it has " " characters or spaces).
This function is a recursion, you can see that in this line:
Code:
del_separations:=del_separations(copy(s,1,k1)+copy(s,k+1,length(s)));
And palindrome_check is the main function in program, and
if just turn the string over and checks if it is the same like enter string, and returns boolean !
So here is the solution in TPW :
Code:
program the_longest_palindrome;
uses wincrt;
var
s,max,temp:string;
i,j:integer;
label top;
function lower_case_string(s:string):string;
var
i:integer;
begin
  for i := 1 to length(s) do
    if (s[i] >='A') and (s[i]<='Z') then
      inc(s[i], 32);
  lower_case_string:=s;
end;

function del_separations(s:string):string;
var
k:integer;
begin
  k:=pos(' ',s);
  if k=0 then
    del_separations:=s
  else
    del_separations:=del_separations(copy(s,1,k-1)+copy(s,k+1,length(s)));
end;

function palindrome_check(s:string):boolean;
var
i:integer;
temp:string;
begin
    temp:='';
   for i:=length(s) downto 1 do
      temp:=temp+s[i];
   if temp=s then
     palindrome_check:=true
   else
     palindrome_check:=false;
end;

begin
top:
writeln('---------------------------------');
writeln('-Program for finding the longest-');
writeln('-    palindrome by Dr DEBCOL    -');
writeln('---------------------------------');
writeln('Enter the word or sentence ?');
readln(s);
s:=lower_case_string(del_separations(s));
max:='';
for i:=1 to length(s) do
  for j:=i to length(s) do
    begin
      temp:=copy(s,i,j);
      if palindrome_check(temp) then
        if length(temp)>length(max) then
          max:=temp;
    end;
if length(max)=1 then
  writeln('This word or sentence does not have palindroms in it !')
else
  writeln('The longest palindrome is ',max);
writeln;
writeln('Press Enter if you want to try again !');
readln;
clrscr;
goto top;
end.
And in Free Pascal IDE :
Code:
program the_longest_palindrome;
var
s,max,temp:string;
i,j:integer;
label top;
function lower_case_string(s:string):string;
var
i:integer;
begin
  for i := 1 to length(s) do
    if (s[i] >='A') and (s[i]<='Z') then
      inc(s[i], 32);
  lower_case_string:=s;
end;

function del_separations(s:string):string;
var
k:integer;
begin
  k:=pos(' ',s);
  if k=0 then
    del_separations:=s
  else
    del_separations:=del_separations(copy(s,1,k-1)+copy(s,k+1,length(s)));
end;

function palindrome_check(s:string):boolean;
var
i:integer;
temp:string;
begin
    temp:='';
   for i:=length(s) downto 1 do
      temp:=temp+s[i];
   if temp=s then
     palindrome_check:=true
   else
     palindrome_check:=false;
end;

begin
top:
writeln('---------------------------------');
writeln('-Program for finding the longest-');
writeln('-    palindrome by Dr DEBCOL    -');
writeln('---------------------------------');
writeln('Enter the word or sentence ?');
readln(s);
s:=lower_case_string(del_separations(s));
max:='';
for i:=1 to length(s) do
  for j:=i to length(s) do
    begin
      temp:=copy(s,i,j);
      if palindrome_check(temp) then
        if length(temp)>length(max) then
          max:=temp;
    end;
if length(max)=1 then
  writeln('This word or sentence does not have palindroms in it !')
else
  writeln('The longest palindrome is ',max);
writeln;
writeln('Press Enter if you want to try again !');
readln;
clrscr;
goto top;
readln;
end.

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: