Stuck working with a program that determines if something is a palindrome. I have most of the code there and struggling putting it all together. Im not sure what to put in my palindrome.cpp file that ties all these together and successfully passes my test cases. Any help is appreciated!
std::string randPalindrome(const int len, const int& correct) {
std::string front = "", back = "";
for (int i = 0; i < len >> 1; ++i) {
const char s = randChar();
front += s;
back += i == correct ? ((s + 1 - 'a') % 26) + 'a' : s;
}
std::reverse(back.begin(), back.end());
return front + back;
}
int main()
{
srand(time(0));
Palindrome* p = new Palindrome();
// test 1: empty strings and single letters are palindromes
assert(p->test_string("") == -1);
assert(p->test_string("a") == -1);
std::cout << "Test 1 passed." << std::endl;
// test 2: correct palindromes, all lowercase, no non-letters
assert(p->test_string("aa") == -1);
assert(p->test_string("bob") == -1);
assert(p->test_string("abba") == -1);
assert(p->test_string("stats") == -1);
std::cout << "Test 2 passed." << std::endl;
// test 3: correct palindromes, not all lowercase, with non-letters
assert(p->test_string("aA") == -1);
assert(p->test_string("Aa") == -1);
assert(p->test_string("Bob") == -1);
assert(p->test_string("Madam") == -1);
assert(p->test_string("A man, a plan, a canal, Panama!") == -1);
assert(p->test_string("Bob: Did Anna peep? Anna: Did Bob?") == -1);
std::cout << "Test 3 passed." << std::endl;
// test 4: returning the correct position of the failed palindromes
assert(p->test_string("ab") == 0);
assert(p->test_string("abca") == 1);
assert(p->test_string("A man, a plan, a canal, Panema!") == 2);
assert(p->test_string("rm -rf /") == 0);
assert(p->test_string("a,,b,,c,,d,,b,,a") == 2);
std::cout << "Test 4 passed." << std::endl;
// test 5: random palindromes so you cannot hard code them
for (int i = 0, len = rand_range, current = (rand() % (len / 2)) - 1; i < rand_range; ++i, len = rand_range, current = (rand() % (len / 2)) - 1)
assert(p->test_string(randPalindrome(len, current)) == current);
std::cout << "Test 5 passed." << std::endl;
CODE FOR PALINDROME.HPP:
#ifndef PALINDROME_HPP
#define PALINDROME_HPP
#include <string>
class Palindrome {
public:
Palindrome() {}
int test_string(const std::string& s);
};
#endif
CODE FOR MYQUEUE.HPP:
#ifndef PALINDROME_HPP
#define PALINDROME_HPP
#include <string>
class Palindrome {
public:
Palindrome() {}
int test_string(const std::string& s);
};
#endif
CODE FOR PQUEUE.HPP
#ifndef PQUEUE_HPP
#define PQUEUE_HPP
#pragma once
/***************************************************************************************************
Creating a PQueue class. Creating contructor/deconstructor, enqueue/dequeue, a peek so we dont *
delete data, and a size and clear to remove from memory *
***************************************************************************************************/
CODE FOR STACK.HPP
#ifndef STACK_HPP
#define STACK_HPP
template <class T>
class Stack
{
private:
public:
Stack() {}
virtual ~Stack() {}
//Functions for performing the push/pop as well as
//peeking so nothing is deleted and the size and clearing when done
virtual void push(const T&) = 0;
virtual T pop() = 0;
virtual const T& peek() const = 0;
virtual void clear() = 0;
virtual int size() = 0;
};
#endif
CODE FOR MYSTACK.HPP
--still writing this one out...will post when complete