Created
June 30, 2017 00:03
-
-
Save jryebread/d0e3eccd9d4caf0c626abf45aa9e148d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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