I changed it to do that. I have included the text file that I am reading from.
This is what I have so far:
Code:
#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <ctype.h>
#include <stdlib.h>
class stack
{
public:
struct node;
typedef node* nodeptr;
struct node
{ char c;
nodeptr next;
};
stack();
void makeStackEmpty();//deletes stack
void push(char);//puts item on top of stack
char pop();//takes off item on top and brings back what was there
bool isEmpty();//is stack empty
void print();//debugging
private:
nodeptr top;
};
stack::stack()
{top=0;
}
void stack::makeStackEmpty()//deletes stack
{
nodeptr temp;
temp=top;
while(temp!=0)
{
top=top->next;
delete temp;
temp=top;
}
}
void stack::push(char x)//puts item on top of stack
{
nodeptr temp;
temp=new node;
temp->c=x;
temp->next=top;
top=temp;
}
char stack::pop()//takes off item on top and brings back what was there
{
nodeptr temp;
char x;
if(isEmpty())
cout << "error -- cannot pop from an empty stack\n";
else
{
temp=top;
x=top->c;
top=top->next;
delete temp;
}
return x;
}
bool stack::isEmpty()//is stack empty
{
if(top==0)
return true;
else
return false;
}
void stack::print()//prints
{
stack t;
char w;
while(!isEmpty())
{
w=pop();
cout << w << " ";
t.push(w);
}
while(!t.isEmpty())
{
w=t.pop();
}
cout << endl;
}
//Globals
ifstream infile;
ofstream outfile;
typedef char string[200];
//---------------------------------------------------------------------------//
int main()
{
int i=0;
stack One, Two, Three; //defines stacks
string x,temp;
infile.open("Pals1.txt", ios::nocreate);//open file to read from
if(!infile)
{
cout << "Error opening file" << endl;
exit(1);
}
outfile.open("palindrome");
while(infile)
{
i++;
infile.get(x[i]);
cout << x[i];
x[i]=toupper(x[i]);
//--------------------------------------------------------------------C++ Board
if(ispunct(x[i])==0&&x[i]!='\n')
{
One.push(x[i]);
Two.push(x[i]);
}
else
{
while ( !Two.isEmpty() ) {
Three.push( Two.pop() );
}
// Three is now the reverse of Two (and One)
// compare each char with its reversed counterpart
bool isPal = true;
while ( !One.isEmpty() ) {
if ( One.pop() != Three.pop() ) {
isPal = false;
}
}
if ( isPal )
{
cout << "palindrome" << endl;
}
else
{
cout << "not palindrome" << endl;
}
}
//---------------------------------------------------------------------/C++ Board
/*if(ispunct(x[i])==0&&x[i]!=' ')
{
s1.push( x[i] );
s2.push( x[i] );
while (s1.isEmpty())
{
s3.push( s1.pop( ) );
}
while (s2.isEmpty( ))
{
if( s2.pop() != s3.pop() )
cout << "...not a palindrome..." << endl;
else
cout << "...is a palindrome..." << endl;
}
*//*One.push(x[i]);
Two.push(x[i]);
Three.makeStackEmpty();
//temp[i]=Two.pop();
Three.push(Two.pop());
Three.print();
*/
/*if()
{
cout << "is a palindrome" << endl;
}*/
}
return 0;
}
//--------------------------------------------------------------------------//