C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

palindrome check programe using stack Rate Topic: -----

#1 xiaolim  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 28-July 08


Dream Kudos: 0

Share |

palindrome check programe using stack

Posted 16 August 2008 - 05:07 AM

hi, sorry to disturb again, currently i have an assignment on stacks and as show in the the title, i need to use stacks to determine palindrome.

i've done part of my code, here it is:

// Write your name, student number, part-time/full time, degree here
#include <string>
#include <iostream>
#define STACKSIZE 80
#define TRUE 1
#define FALSE 0
using namespace std;

class Stack 
{
   public:
		  void StackInit();
		  int IsStackEmpty();
		  int IsStackFull();
		  int StackPush(char c);
		  char StackPop();
		  char ViewStackTop();
   private:
		  char myStack[80];
		  int top;
};

string pstring;

int main()
{
	// write your code here
	cout << "This Is A Palindrome Check Programe" << endl;
	cout << "Please Enter Anything For Palindrome Check" << endl;
	
	cin >> pstring;
	

	
	
	system("pause");
	return 0;
}

void Stack::StackInit()
{
  top = -1;
}

int Stack::IsStackEmpty()
{
  if (top == -1)
	return TRUE;
  return FALSE;
}

int Stack::IsStackFull()
{
  if (top == (STACKSIZE - 1))
	return TRUE;
  return FALSE;
}

int Stack::StackPush(char c)
{
  if (top == (STACKSIZE - 1))
	return FALSE;
  myStack[++top] = c;
  return TRUE;
}

char Stack::StackPop()
{
  if (top == -1)
	return '\0';
  return myStack[top--];
}

char Stack::ViewStackTop()
{
  if (top == -1)
	return '\0';
  return myStack[top];
}





alright, i am stuck again, i need to ignore space, punct and digits, i know using isdigit / ispunct / isspace. however i ahh.. duno where to put them. hmmm, simple guide will do. thanks, meanwhile, i will be doing search on google as well. please help. thanks
Was This Post Helpful? 1


#2 ibaraku  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 12-May 07


Dream Kudos: 0

Re: palindrome check programe using stack

Posted 16 August 2008 - 11:33 PM

I guess you could write your own removepunctuation function


string removePunct(const string& s, const& punct)
{
   string noPunct;//empty string
   int sLength = s.length();
   int punctLength = punct.length();
   for (int i = 0; i < sLength; i++)
   {
	  string aChar = s.substr(i, 1);//A one char string
	  int location = punct.find(aChar, 0);//
	  //finds the location of successive characters

		 if (location < 0 || location >= punctLength)
		 {
			noPunct = noPunct + aChar;//aChar is NOT punct, so keep it...
		 }
	  }
   return noPunct;

}




hope that helps
Was This Post Helpful? 0
  • +
  • -

#3 xiaolim  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 28-July 08


Dream Kudos: 0

Re: palindrome check programe using stack

Posted 18 August 2008 - 06:10 AM


#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#define STACKSIZE 80
#define TRUE 1
#define FALSE 0
using namespace std;

class Stack 
{
   public:
		  void StackInit();
		  int IsStackEmpty();
		  int IsStackFull();
		  int StackPush(char c);
		  char StackPop();
		  char ViewStackTop();
   private:
		  char myStack[80];
		  int top;
};

  
char input[100], output[100];
Stack s;

int main()
{
	char c;
	s.StackInit();
	// write your code here
	cout << "This Is A Palindrome Check Programe" << endl;
	cout << "Please Enter Anything For Palindrome Check" << endl;
	fflush(stdin);
	cin.getline(input, 100);
	
	for (int i=0;i<strlen(input);i++)
	

	//check input is upper and convert to lower
	while (input[i])
	{
	c=input[i];
	putchar (tolower(c));
	i++;
	}
	
	//check input have punct or space
	if (isspace(input[i]))
	
	
	
	//pass converted string to a temp
	c = input[i];

	//check input is only alpha and push to stack
	if(isalpha(input[i]))   
	s.StackPush(input[i]);
	i++;

	
	//compare 1st 2 char with last 2 char
	if strcmp( ?????? )
	
	//output
	cout << "The String " << c << "Is A Palindrome." << endl;
	
	else

	cout << "The String " << c << "Is Not A Palindrome." << endl;	
	
	
	
	system("pause");
	return 0;
}

void Stack::StackInit()
{
  top = -1;
}

int Stack::IsStackEmpty()
{
  if (top == -1)
	return TRUE;
  return FALSE;
}

int Stack::IsStackFull()
{
  if (top == (STACKSIZE - 1))
	return TRUE;
  return FALSE;
}

int Stack::StackPush(char c)
{
  if (top == (STACKSIZE - 1))
	return FALSE;
  myStack[++top] = c;
  return TRUE;
}

char Stack::StackPop()
{
  if (top == -1)
	return '\0';
  return myStack[top--];
}

char Stack::ViewStackTop()
{
  if (top == -1)
	return '\0';
  return myStack[top];
}





alright, i tried, i briefly list out what i should do.

1. StackInit.
2. promt user to enter anything.
3. check if is uppercase, if yes, convert to lowercase then move to next char
4. ignore all space and punct.
5. pass the converted string to another temp var.
6. check if is only alpha, if yes, push char in stack. if not, ignore go to next char
7. pop the last 2 char.
8. compare 1st 2 char with last 2 char, if is similar, then output result, if not, display msg.

thanks, but i don;t need to remove punct, i just need to ignore them.
so, quite alot of problem, maybe someone can help me check out n tell me where i go wrong? i keep can't compile on the "tolower" part. thanks alot
Was This Post Helpful? 1



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users