Use C++ , Palindrome Detector palindromes.txt Problem 1. (15 points) Palindrome
ID: 3780076 • Letter: U
Question
Use C++ , Palindrome Detector
palindromes.txt
Problem 1. (15 points) Palindrome Detector. A palindrome is a phrase that reads the same forwards as it does backwards. For example a man, a plan, a canal, Panama is a palindrome. Write a program that uses a stack to check for palindromes in each line of a text file. Try your program on the example text file "palindromes txt'. Your program should output the palindromes that it finds in the document. For example: FindPalindromesC"palindromes.txt") will display "a man, a plan, a canal, Panama" is a palindrome "Don't nod" is a palindrome II aco Cat!" is a palindrome Using only a fixed number of stacks, the stack ADT functions, and a fixed number of int and char variables, write the function FindPalindromes to display all palindromes found in a given text file. Feel free to use either a LinkedList or an array to implement the Stack. What is the complexity of your function?Explanation / Answer
PROGRAM CODE:
/*
* palindromeDetector.cpp
*
* Created on: 02-Dec-2016
* Author: kasturi
*/
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <stdio.h>
using namespace std;
string testStrings[50];
string actualStrings[50];
int numberOfStrings;
class stack
{
char stk[40];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 39)
{
cout <<" stack over flow";
return;
}
stk[++top]=x;
}
char pop()
{
if(top <0)
{
cout <<" stack under flow";
return '';
}
char element= stk[top];
stk[--top];
return element;
}
};
void findPalindromes()
{
for(int i=0; i<numberOfStrings; i++)
{
stack st;
string line = testStrings[i];
int lineLength = line.length();
int counter = 0;
bool ispalindrome = true;
while(counter<lineLength)
{
st.push(line.at(counter));
counter++;
}
counter = 0;
while(counter<lineLength)
{
if(st.pop() != line.at(counter))
{
ispalindrome = false;
break;
}
counter++;
}
if(ispalindrome)
{
cout<<"""<<actualStrings[i]<<"""<<" is a palindrome ";
}
}
}
int main()
{
ifstream infile("palindromes.txt");
string line;
int counter = 0;
while(getline(infile, line))
{
actualStrings[counter] = line;
testStrings[counter] = "";
for(int i=0; i<line.length(); i++)
{
char a = line.at(i);
int asciiNumber = a;
if((asciiNumber>=97 && asciiNumber<=122) || (asciiNumber>=65 && asciiNumber<=90))
{
testStrings[counter] = testStrings[counter] + std::tolower(a, std::locale());
}
}
counter++;
}
numberOfStrings = counter;
findPalindromes();
return 0;
}
OUTPUT:
"A man, a plan, a canal, Panama." is a palindrome
"Don't nod" is a palindrome
"Taco Cat!" is a palindrome
"Rats live on no evil star." is a palindrome
"Neil, a trap! Sid is part alien!" is a palindrome
"Step on no pets." is a palindrome
"Dammit, I'm mad!" is a palindrome
"Madam, I'm Adam." is a palindrome
"Madam, in Eden, I'm Adam." is a palindrome
"Rise to vote, sir." is a palindrome
"Never odd or even" is a palindrome
"If I had a hi-fi" is a palindrome
"Yo, banana boy!" is a palindrome
"Do geese see God?" is a palindrome
"No devil lived on." is a palindrome
"Ah, Satan sees Natasha." is a palindrome
"Lewd did I live & evil I did dwel!" is a palindrome
"A dog, a panic in a pagoda" is a palindrome
"Was it a cat I saw?" is a palindrome
"Was it a car or a cat I saw?" is a palindrome
"A Toyota's a Toyota." is a palindrome
"No lemons, no melon" is a palindrome
"Now I see bees, I won." is a palindrome
"Ma is as selfless as I am." is a palindrome
"Nurse, I spy gypsies-run!" is a palindrome
"A dog, a plan, a canal, pagoda" is a palindrome
"Was it Eliot's toilet I saw?" is a palindrome
"No, sir, away! A papaya war is on!" is a palindrome
"Go hang a salami, I'm a lasagna hog." is a palindrome
"I, madam, I made radio! So I dared! Am I mad? Am I?" is a palindrome
"Swap God for a janitor, rot in a jar of dog paws." is a palindrome
"Eva, can I see bees in a cave?" is a palindrome
"So many dynamos!" is a palindrome
"Red rum, sir, is murder." is a palindrome
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.