1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
namespace compsci_mystring {
class MyString {
public:
MyString(const char* inString);
MyString();
MyString(const MyString& copyMe);
~MyString();
friend std::ostream& operator<<(std::ostream& out, const MyString& printMe);
friend std::istream& operator>>(std::istream& in, MyString& readMe);
void read(std::istream& in, char delimeter);
static const int MAX_INPUT_SIZE = 127;
char operator[] (int index) const;
char& operator[](int index);
friend bool operator<(const MyString& left, const MyString& right);
friend bool operator>(const MyString& left, const MyString& right);
friend bool operator<=(const MyString& left, const MyString& right);
friend bool operator>=(const MyString& left, const MyString& right);
friend bool operator==(const MyString& left, const MyString& right);
friend bool operator!=(const MyString& left, const MyString& right);
MyString operator=(const MyString& right);
friend MyString operator+(const MyString& left, const MyString& right);
MyString operator+=(const MyString& right);
int length() const;
private:
char* str;
};
}
#endif
#include <iostream>
#include <cstring>
#include <cctype>
#include "mystring.h"
using namespace std;
using namespace compsci_mystring;
bool isAPalindrome(MyString inString, int start, int end);
int main()
{
const int SIZE = 256;
MyString inString[SIZE]{};
char quit[] = { "quit" };
int start, end;
cout << "Enter a string or type quit: "; // test: Able was I, ere I saw Elba
cin.getline(inString, SIZE);
while (strcmp(inString, quit) != 0) {
cout << inString;
if (isAPalindrome(inString, inString[0], inString[strlen(inString) - 1])) {
cout << " is a palindrome." << endl;
}
else cout << " is not a palindrome." << endl;
cout << "Pick 2 elements, everything in between "
<< "and including the elements wil be checked for palindrome." << endl;
cout << "Starting element: ";
cin >> start;
cout << "Ending element: ";
cin >> end;
if (isAPalindrome(inString, start, end)) {
while (start <= end) {
cout << inString[start];
start++;
}
cout << ": is a palindrome." << endl;
}
else {
while (start <= end) {
cout << inString[start];
start++;
}
cout << ": is not a palindrome." << endl;
}
cin.ignore();
cout << "Enter a string or type quit: ";
cin.getline(inString, SIZE);
}
return 0;
}
bool
isAPalindrome(MyString inString, int start, int end)
{
if (start < end) {
if (isspace(inString[start]) || ispunct(inString[start]))
return isAPalindrome(inString, start + 1, end);
else if (isspace(inString[end]) || ispunct(inString[end]))
return isAPalindrome(inString, start, end - 1);
if (toupper(inString[start]) != toupper(inString[end]))
return false;
return isAPalindrome(inString, start + 1, end - 1);
}
return true;
}
|