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.