I want to use stacks to test whether the input is a palindrome or not. Right now, for the input, I'm just using a six letter word, eg. abccba.
What I need to do is make it so it's a loop instead of only running three times so any length of word can be tested. If the length of the word is odd, it needs to pop the middle number and just compare what's left. (I know I have some unused variables because I meshed 2 of my programs together.)
#include <iostream>
#include <math.h>
char convert(char x); //defining the function for converting all characters to lowercase
usingnamespace std;
class IntStack
{
public:
IntStack(int num)
{
top = 0;
maxelem = num;
list1 = newchar[maxelem];
list2 = newchar[maxelem];
}
//**********************************************
void push(int t)
{
if (top == maxelem) return;
list1[top++] = t;
}
//*********************************************
char StackTop()
{
//assert(top!=0);
return list1[top-1];
}
//*********************************************
int pop()
{
if (top == 0)
return -1;
return list1[--top];
}
//***************************************************
void display()
{
if (top == 0)
{
cout << "(empty)\n";
return;
}
for (int t=0 ; t < top ; t++)
cout << list1[t] << " ";
cout << "\n";
}
int empty() { return top == 0; }
public:
char *list1;
char *list2;
int top;
int maxelem;
};
int main()
{
IntStack *list1 = new IntStack(100);
IntStack *list2 = new IntStack(100);
char d;
char Sentencein[50]; //InputArray has a limit of 51 characters
char Sentenceout[50]; //OutputArray has a limit of 51 characters
char ch;
int i = 0;
int j = 0;
int l = 0;
char b;
bool IsPalendrome = true;
bool isodd = false;
//asking the user to input a sentence.
//this sentence is placed into the sentencein
//what is in sentencein is moved to ch
cout << "Please enter a sentence. This program will test to see if a sentence is a palindrome." << endl;
cin.get(ch);
while (ch != '\n') //when the input is not the return key, this loop takes each character, passes it through the user defined function, increases the counter, and repeats until the return key t
{
if (ch !=' ')
{
ch = convert(ch);
j++;
list1->push(ch);
}
cin.get(ch);
}
cout << j;
if (j % 2 != 0)
isodd=true;
list1->display();
d=list1->StackTop();
cout <<" The top is "<< d <<endl;
list2->push(d);
list2->display();
list1->pop();
d=list1->StackTop();
cout <<" The top is "<< d <<endl;
list2->push(d);
list1->display();
list1->pop();
d=list1->StackTop();
cout <<" The top is "<< d <<endl;
list2->push(d);
list1->display();
list1->pop();
list1->display();
cout << " list2 " << endl;
list2->display();
return 0;
}
char convert(char x) //defining the function for converting all char to lower case
{
char l; // defining the character l
l = tolower(x); // changes the whole string to lowercase
return l;
}
Your program is far too complicated. All you need to do is get a string, push each character on to the stack. Create a second string and pop all characters from the stack and add it to the second string. Finally you just compare to two strings.