Stack & Palindrome

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.)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <math.h>

char convert(char x);  //defining the function for converting all characters to lowercase

using namespace std;


class IntStack
{
public:
   IntStack(int num)
   {
       top = 0;
       maxelem = num;
       list1 = new char[maxelem];
       list2 = new char[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;

}
 
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

class IntStack
{
public:
  IntStack (int num)
  {
    top = 0;
    maxelem = num;
    list1 = new char[maxelem];
    list2 = new char[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 stack (100);

  string input, output;
  cout << "Enter text: ";
  getline (cin, input);
 
  for (size_t i = 0; i < input.length (); i++)
  {
    stack.push (input[i]);
  }
  while (!stack.empty ())
  {
    output += (char)stack.pop ();
  }
  if (input == output)
  {
    cout << input << " and " << output << " are palindromes\n\n";
  }
  else
    cout << input << " and " << output << " are not palindromes\n\n";

  system ("pause");
}
Topic archived. No new replies allowed.