I have been writing a palindrome finder in C++ and I have succeeded in writing one that is.... basic to say the least.
I am looking simply to increase the runspeed of the program, right now it takes about ~1m 5s to run a test for palindromes / 2 word palindromes on a 1500 word wordlist using the functions that I have. I would like to try running it on a much larger file but fail to see where I can optimize further?
Any help would be appreciated: P.S. This is not for school, just for leisure.
#include <iostream>
#include <ostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
bool isPal(string);
int main() {
vector<string> sVec;
vector<string> sWords;
vector<string> sTwoWords1;
vector<string> sTwoWords2;
char myfile[256]="/home/Damien/Test.txt";
ifstream fin;
string str;
fin.open(myfile);
if(!fin){
cout << "fin failed";
return 0;
}
while(fin){
fin >> str;
sWords.push_back(str);
if(!fin){
break;
}
if(isPal(str)){
sVec.push_back(str);
}
else{
getline(fin, str);
}
}
reverse(sVec.begin(), sVec.end());
for(int i =0; i < sVec.size(); i++){
cout << sVec[i] << " is a Palindrome " <<endl;
}
// Test 2
for(int i=0; i<sWords.size(); i++){
for(int j=(i+1); j<sWords.size(); j++){
str = sWords[i]+sWords[j];
if(isPal(str)){
sTwoWords1.push_back(sWords[i]);
sTwoWords2.push_back(sWords[j]);
}
}
}
fin.close();
for(int i=0; i<sTwoWords1.size(); i++){
cout << sTwoWords1[i] << " and " << sTwoWords2[i] << " are palindromic. \n";
}
return 0;
}
bool isPal(string& testing) {
return std::equal(testing.begin(), testing.begin() + testing.size() / 2, testing.rbegin());
}