PalindromeExercise.cpp

Go to the documentation of this file.
00001 #include "macros.h" // needs to be declared as first entry in .cpp or .h file!
00002 #include <string>
00003 #include <iostream>
00004 using namespace std;
00005 #include "AbstractExercise.h"
00006 
00012 class PalindromeExercise : public AbstractExercise {
00013 public:
00014 
00020     PalindromeExercise(string name) : AbstractExercise(name){}
00021 
00022 protected:
00023 
00028     void execute();
00029 
00034     void getInput();
00035 
00036 private:
00037     string fWord;
00038     static bool isPalindrome(const string& strToTest);
00039 
00040 };
00041     
00047 namespace {
00048     PalindromeExercise proto("PalindromeExercise");
00049 }
00050 
00051 void PalindromeExercise::getInput(){
00052     cout << "Enter string to test: "<< endl;
00053     cin >> fWord;
00054 }
00055 
00056 void PalindromeExercise::execute(){
00057     cout << "<" << fWord << ">" << " is " << (isPalindrome(fWord) ? "a " : "no ") << "palindrome." << endl;
00058 }
00059 
00060 bool PalindromeExercise::isPalindrome(const string& strToTest){
00061     const int STR_LENGTH = strToTest.length();
00062     bool stillIsPalindrome = true;
00063 
00064     for(int i=0;i<(STR_LENGTH / 2);i++){
00065         stillIsPalindrome = strToTest[i] == strToTest[STR_LENGTH-1-i];
00066         if(!stillIsPalindrome){
00067             break;
00068         }
00069     }
00070     return stillIsPalindrome;
00071 }

Generated on Thu Jul 3 19:12:45 2008 for Exercise Framework by  doxygen 1.5.1