Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried to make a program that checks for a string whether it is a palindrome or not but it is giving error after I give input
For Example if i give radar word
it should display Palindrome
but it is not working properly there are no syntax error please help

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;
			rear->prev=front->next;
			newnode->next=NULL;
			newnode->prev=rear;
			rear=newnode;
		}
		else
		{
			front->next=newnode;
			front->prev=NULL;
			newnode->next=NULL;
			newnode->prev=front;
			rear->prev=front;
			rear=newnode;
		}
		size++;
	}
	int si()
	{
		return size;
	}
	int check()
	{
		int count;
		node *p=front->next;
	    	while(p!=NULL)
	    	{
	    		if(p->v==rear->v)
	    		{
	    			count++;
	    		}
	    		p->next;
	    		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();
	if(s%2==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 14:31pm

This is your homework, nor ours - and getting it working is part of the task.
So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Quote:
but it is giving error

Which error message ?

There is a tool made especially to help programmer to understand what their code is really doing when it don't do what they expect, give it a try.

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[^]
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
 

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