Skip to content

Instantly share code, notes, and snippets.

@jryebread
Created June 30, 2017 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jryebread/d0e3eccd9d4caf0c626abf45aa9e148d to your computer and use it in GitHub Desktop.
Save jryebread/d0e3eccd9d4caf0c626abf45aa9e148d to your computer and use it in GitHub Desktop.
#include "PalCheck.h"
#include "Queue.h"
#include <string>
bool PalCheck::isPalindrome(Queue<char> pq)
{
std::string nline = "";
std::string rline = "";
int n = pq.ifront();
int r = pq.iback();
while (n != r && r != n+1) //end at middle or if string is even break at r = n+1
{
//work from the front and back, iterating and adding those chars
//to our nline and rline strings
nline += pq.peek();
pq.dequeue();
rline += pq.peekBack();
pq.dequeueBack();
n++;
r--;
}
//once n == r break, and compare strings to check for pal
if (nline == rline)
return true;
return false;
}
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "PalCheck.h"
#include "LinkedList.h"
#include "Queue.h"
using namespace std;
int main()
{
cout << "=========PALINDROMES==========" << endl;
PalCheck p;
//Make the Queue of Chars
string line;
ifstream myfile("pal.txt");
ofstream outputFile("outputpal.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
Queue<char> pq;
for (int j = 0; j < line.length(); ++j)
{
line[j] = tolower(line[j]);
if(line[j] != ' ')
pq.enqueue(line[j]);
}
if (p.isPalindrome(pq))
outputFile << line << endl;
}
}
else cout << "Unable to open file\n";
myfile.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment