Hello everyone,

I have to create a Palindrome Programme in Pascal using Pointers and Queues.

A Palindrome is a word which when spelt backwards or forward if the same ex RACECAR or RADAR.

I am having a problem actually creating the Palindrome Part of the Code.

Thus Far, this is what I have

PROGRAM Palindrome (input, output);

TYPE

 pQueue = ^tQueue;

 tQueue = RECORD

      prev : pQueue;

      data : String [80];

      next : pQueue;

 END;

VAR

 Front, Rear, Temp : pQueue;

 i : String [80];

 Choice : CHAR;

 Count : INTEGER;

 Palid : BOOLEAN;

{***************************************************************************}

PROCEDURE add;

BEGIN

 NEW (Temp);

 Temp^.data := i;

 Temp^.next := nil;

 IF Front = nil THEN

    Front := Temp

 ELSE Rear^.next := Temp;

 Rear := Temp;

END;

{***************************************************************************}

PROCEDURE view;

BEGIN

 Temp := Front;

 WHILE Temp <> Nil DO

 BEGIN

      WRITELN (Temp^.data);

      Temp := Temp^.next;

 END;

END;
{***************************************************************************}
PROCEDURE destroy;

BEGIN

 Temp := Front;

 WHILE Temp <> nil DO

 BEGIN

       Temp := Temp^.next;

       DISPOSE  (Front);

       Front := Temp;

 END;

END;
{***************************************************************************}

PROCEDURE Entry;

BEGIN

 Front := Nil;

 WRITELN ('Please Enter A Word');

 READLN (i);

 add;

 view;

 destroy;

END;

{***************************************************************************}

BEGIN

 Count := 0;

 WRITELN ('Would You Like To Enter A Word?');

 READLN (Choice);

 WHILE (Choice = 'Y') OR (Choice = 'y') DO

 BEGIN

       IF (Choice = 'Y') OR (Choice = 'y') THEN

          Entry;

       WRITELN ('Would You Like To Enter Another Word?');

       READLN (Choice);

 Count := Count + 1;

 END;

 WRITELN (Count);

END.

I am having Problems creating the Palindrome Part. Can anyone help me out here?

I am not allowed to use arrays. Just Queues and Pointers.

Thanking you in adavnce for your assistance.

Hi,

Ths last " IF (Choice = 'Y') OR (Choice = 'y') THEN " is unnecessary, the while loop's conditional takes care of that. You seem to have implemented the queue correctly but why is you TQueue's Data is a String of length 80 ? If your code is to check whether a given word is a palindrome then the data should be a single char and the whole queue should contain the entered word char by char; then to check you should compare the chars using two pointers one going forward from FRONT and other going backward from REAR at the same time, if all chars are equal than this is a palindrome.

Loren Soth

Lord Soth, Thanks for replying and helping me.

However, can you show me a sample code of how to create the Palindrome? I have been trying and having some problems.

Thanks

Hi,

Do you need to create Palindromes or check wether the word entered by user is a palindrom ?

Loren Soth

I need for the program to check if the word entered is a palindrome or not.

Thanks

with char arrays, it could be:

ispalindrome := true;
 
for i := 0 to arr.length /2 do
ispalindrome := ispalindrome and (arr[i] == arr[arr.length - i]);

just take the same principle with memory addresses, but bear in mind that only works if the word has an even number of characters.

Matt Evans and Lord Soth, thanks for all your advice and assistance. I was able able to get the program working but it had a couple of bugs. Still, your advice and help allowed me to be on the right track, thanks again.

I will post the code I came up with soon to show you.

function palindromcheck (s:string) :bool;
var l,i:integer;
begin
l:=Length(s);
for i:=0 to (l div 2) do begin
if comparetext(s[i+1], s[l-i])<>0 then Result:=false;
end;

end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if palindromcheck(Edit1.Text) = true then begin
label1.Caption := 'schönes Polinom';
end
else begin
label1.Caption := 'kein schönes Polinom';
end;
end;

hf&gl

commented: This is a very old thread, consired code snippet if you want -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.