I m once again having problems. I need to get a sentence into an array that I can work with I need to check it for being a palindrome. I ve got it all set up and debugged(or did before playing with it) but cant find the right way to get this sentence put into the stack and que like I need it. Here is the code pls help if you can tks in advance for anything you can give.
Code:
#include <iostream>
const int maxsize = 50;
class stackchar
{
public: stackchar();
void enqueue(char ch);
char front();
void dequeue();
bool isfull();
bool isempty();
bool isempty2();
bool isfull2();
void push(char newchar);
void pop();
char topelement();
private:
int frontelement;
int rear;
char s1[maxsize];
char s2[maxsize];
char s3[maxsize];
int top;
int vecsize;
int top2;
};
-------------------------------------------------------------------------
#include "s2p1.h"
#include <iostream>
int vecsize = maxsize;
stackchar::stackchar()
{
rear = frontelement = 0;
}
bool stackchar::isempty()
{
return(rear == frontelement);
}
bool stackchar::isfull()
{
return(rear+1%vecsize==frontelement);
}
void stackchar::enqueue(char ch)
{
rear =(ch)%vecsize;
}
char stackchar::front()
{
return s1[frontelement+1%vecsize];
}
void stackchar::dequeue()
{
frontelement=(frontelement+1)%vecsize;
}
bool stackchar::isfull2()
{
return(top==maxsize);
}
bool stackchar::isempty2()
{
return(top==-1);
}
void stackchar::push(char newchar)
{
s2[++top]=newchar;
}
char stackchar::topelement()
{
return s2[top];
}
void stackchar::pop()
{
top--;
}
}
-------------------------------------------------------------
#include "s2p1.cpp"
#include<iostream>
#include<fstream.h>
#include<string>
#include <istream>
int main()
{
stackchar s1;
stackchar s2;
stackchar s3;
int frontelement;
int rear;
char ch[50];
int top2;
cout<<"Please enter a sentence to be tested $ to exit: ";
cin.getline(ch, 50);
while (!s2.isfull())
{
s2.push(ch);
s1.enqueue(ch);
}
while (!s2.isempty2() && s2.topelement() == s1.front())
{
s2.pop();
s1.dequeue();
}
if (!s2.isempty2())
{
cout<<"Sentence is not a palindrome";
}
else
{
cout<<"Sentence is a palindrome when applying stack and queue";
}
return 0;
}
I haven't worked much with char. and that is where my problem lies I think.