This is Part 3 of the 7-part Exercise 6-0. Click here to see the other parts.
Exercise 6-0 (Part 3 / 7)
Relating to section 6.1.2 of the textbook (page 105). Implement and test out the home-made isPalindrome function (created using the equal utility). Note that this is a more neat / compact version to the created in my previous Solution to Exercise 5-10.
The Project
This section summarises the partitioned program in the form of C++ source and header files.

Source File List
Header File List
Source Files
main.cpp
#include <iostream> // cin, cout, endl
#include <string> // string
#include <vector> // vector
#include "vcout.h" // vcout
#include "isPalindrome.h" // isPalindrome
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
string word;
vector<string> words;
vector<string> palindromes;
// Populate vector
while (cin >> word) {
if (isPalindrome(word))
palindromes.push_back(word);
}
// Print palindrome
cout << "\nThese are the palindromes identified: " << endl;
vcout(palindromes);
return 0;
}
isPalindrome.cpp
#include <string>
using std::string;
bool isPalindrome(const string& s)
{
return equal(s.begin(), s.end(), s.rbegin() );
}
vcout.cpp
#include <iostream>
#include <string> // string
#include <vector> // vector
using std::cout;
using std::endl;
using std::string;
using std::vector;
int vcout(const vector<string>& v)
{
for (vector<string>::const_iterator i = v.begin(); i != v.end(); ++i)
cout << (*i) << endl;
return 0;
}
Header Files
isPalindrome.h
#ifndef GUARD_ISPALINDROME_H #define GUARD_ISPALINDROME_H #include <string> bool isPalindrome(const std::string&); #endif // GUARD_ISPALINDROME_H
vcout.h
#ifndef GUARD_VCOUT_H #define GUARD_VCOUT_H #include <string> #include <vector> int vcout(const std::vector<std::string>&); #endif // GUARD_VCOUT_H
Test Program
I know enter a list of words. The program should automatically identify and display the palindrome. Note that for simplicity sake the program is case sensitive – in this test I shall strictly stick to lower case letters. I shall type in the following lines:
- good morning bob
- lol what a beautiful moon
- eeeeeeeee aaaaaaa banana
The the program should identify these words as palindromes (as they spell the same in both directions): bob, lol, a, eeeeeeeee, aaaaaaa
good morning bob lol what a beautiful moon eeeeeeeee aaaaaaa banana ^Z These are the palindromes identified: bob lol a eeeeeeeee aaaaaaa