Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I made this program to find whether the string entered is palindrome or not but when I enter a Palindrome word such as RADAR it gives me not a Palindrome in the output

What I have tried:

#include<iostream>
using namespace std;
class node{
	public:
	char v;
	node *next;
    node *prev;
};
class queue{
	node *front,*rear;
	int size;
	public:
	queue()
	{
		front=new node();
		rear=NULL;
		size=0;
	}
	void enqueue(char a)
	{
		node *newnode=new node();
		newnode->v=a;
		if(rear!=NULL)
		{
			rear->next=newnode;
			newnode->prev=rear;
			newnode->next=NULL;
			rear=newnode;
		}
		else
		{
			front->next=newnode;
			front->prev=NULL;
			newnode->next=NULL;
			newnode->prev=front;
			rear=newnode;
		}
		size++;
	}
	int si()
	{
		return size;
	}
	int check()
	{
		int count;
		node *p=new node();
		p=front->next;
	    	while(p!=NULL && rear!=NULL)
	    	{
	    		if(p->v==rear->v)
	    		{
	    			count++;
	    		}
	    		p=p->next;
	    		rear=rear->prev;
	    	}
	    	return count;
	    
	}
};
int main()
{
	queue q1;
		int s,num;
	cout<<"Enter size of string :";
	cin>>s;
		char a[s],n;
	cout<<"Enter string :";
	cin>>a;
	for(int i=0;i<s;i++)
	{
		n=a[i];
		q1.enqueue(n);
	}
	
	num=q1.check();
	int ns=s%2;
	if(ns==1)
	{
	if(num==(s-1))
	{
		cout<<"Palindrome"<<endl;
	}
	else
	{
		cout<<"not a Palindrome";
	}
    }
    else
    {
    	if(num==s)
	{
		cout<<"Palindrome"<<endl;
	}
	else
	{
		cout<<"not a Palindrome";
	}
    }
	return 0;
}
Posted
Updated 29-Oct-17 15:12pm
Comments
Richard MacCutchan 30-Oct-17 5:08am    
Please do not repost the same question.

1 solution

Quote:
Palindrome using queues

It is your right to make things as complicated as you want, but don't complain if nobody want to follow you on this track.
To check if a string is a palindrome, I need 2 variables

Albert Einstein said:
Everything should be made as simple as possible, but no simpler.


There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
CPallini 30-Oct-17 5:33am    
5."It is your right to make things as complicated as you want, but don't complain if nobody want to follow you on this track." :-)
Patrice T 30-Oct-17 5:40am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900