Write a program that reads a line of text, changes each uppercase letter to lowe
ID: 3681006 • Letter: W
Question
Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and into a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward).
You use templates stack, queue , functions from them, string::length() function, also use getline_portable() function for Eclipse instead of getline(). Function getline_portable() is provided. You give option to user ignore or not ignore spaces ‘ ‘ in the line of text.
For example:
Check if a line is a palindrome. Ignore spaces? y/n y
Input line to check
a W rt r wa
Inputed line {a W rt r wa} is a palindrome
Check if a line is a palindrome. Ignore spaces? y/n n
Input line to check
a w rtr wa
Inputed line {a w rtr wa} is not a palindrome
Input line to check see you y o u e e s Inputed line {see you y o u e e s} is not a palindrome
Check if a line is a palindrome. Ignore spaces? y/n y
Input line to check s
ee you uoy e e s
Inputed line {see you uoy e e s} is a palindrome
Explanation / Answer
#include "Stack.h"
#include < fstream >
#include < cctype >
#include < string >
#include < iomanip >
#include < algorithm >
#include < cstdlib >
#include "Queue.h"
#include < iostream >
using namespace std ;
int main ( )
{
Stack Str ;
Queue Q ;
string stng ;
int i = 0 ;
char string[MAXIMUM] ;
bool OUTCOME = false ;
strcpy ( string , " " ) ;
cout <<" Please enter a line of text : "<< endl ;
cin.getline ( string , 100 ) ;
for (int j= 0 ;j <stng.size ( ) ;++j )
{
if ( stng[j] >= 'A' && stng[j] <='Z' )
stng +=( stng[j] |32 ) ;
else
if ( stng[j] >= 'a' &&s[j] <='z' )
stng += ( stng[j] & ( ~32 ) ) ;
else
stng +=stng[j] ;
}
while ( string[i] != NULL )
{
Str.push ( string[i] ) ;
Q.appendQu ( string[i] ) ;
i++ ;
}
while ( i > 0 )
{
if ( Str.topNpop ( ) == Q.primaryNremoveq ( ) )
{
OUTCOME = true ;
}
else
{
OUTCOME = false ;
break ;
}
i-- ;
}
if ( OUTCOME == true )
{
cout << string << " This is a palindrome " ;
}
else
{
cout << string << " This is not a palindrome " ;
}
cout << stng << stng << endl ;
return 0 ;
}
-----------------------------------------------------------------------------------------------
Stack.h
#ifndef _Stack_h
#define _Stack_h
const int MAXIMUM = 50 ;
typedef char ElementStack ;
class Stack
{
public:
Stack ( )
{
top = -1 ;
aray[MAXIMUM] = 0 ;
}
void push ( ElementStack &letter ) ;
ElementStack topNpop ( ) ;
bool empty ( ) const ;
private:
ElementStack aray[MAXIMUM] ;
int top ;
} ;
inline void Stack::push ( ElementStack &letter )
{
if ( top < MAXIMUM )
{
top++ ;
aray[top] = letter ;
}
else
{
cout << " Stack is full . " ;
}
}
inline ElementStack Stack::topNpop ( )
{
if ( top > -1 )
{
return ( aray[top] ) ;
top-- ;
}
else
{
cout << " Stack is empty .. " ;
return ( 0 ) ;
}
}
inline bool Stack::empty ( ) const
{
bool OUTCOME = false ;
if( top == -1 )
{
OUTCOME = true ;
return ( OUTCOME ) ;
}
else
{
return ( OUTCOME ) ;
}
}
#endif
--------------------------------------------
Queue.h
#ifndef _Queue_h
#define _Queue_h
const int MAXIMUM = 50 ;
typedef char ElementQueue ;
class Queue
{
public:
Queue ( )
{
primary = 0 ,rear = 0 ;
aray[MAXIMUM] = 0 ;.
}
void appendQu ( ElementQueue &letter ) ;
ElementQueue primaryNremoveq ( ) ;
private:
ElementQueue aray[MAXIMUM] ;
int primary ,rear ;
} ;
inline void Queue::appendQu ( ElementQueue & letter )
{
if ( primary != ( rear + 1) % MAXIMUM )
{
aray[rear] = letter ;
rear = ( rear + 1 ) % MAXIMUM ;
}
else
{
cerr << " Error Queue is full " ;
}
}
inline ElementQueue Queue::primaryNremoveq ( )
{
if ( primary != rear )
{
return ( aray[primary] ) ;
primary++ ;
}
else
{
cout << " Queue is empty .. " ;
return ( 0 ) ;
}
}
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.