A Programming Example

This is an example illustrating the use of strings. A palindrome is a string that reads the same front to back as it does back to front. This project treats all letters as lower case letters, and ignore all whitespaces and punctuations.

Some examples of palindromes are:

radar
deed
mom
dad
racecar
Able was I 'ere I saw Elba.
I Love Me, Vol. I.
Madam, I'm Adam.
A man, a plan, a canal, Panama.
Rats live on no evil star.
Dammit I'm mad!
As I pee, sir, I see Pisa.
Draw pupil's lip upward.
Ma is as selfless as I am.
Was it a car or a cat I saw?
Do geese see god?
A Santa dog lived as a devil god at NASA.
Was it Eliot's toilet I saw?

//Program to test for palindrome property
#include <iostream>
#include <string>
#include <cctype>
// needed to converge upper to lower case
using namespace std;

void swap(char& lhs, char& rhs);
//swaps char args corresponding to parameters lhs and rhs

string reverse(const string& str);
//returns a copy of arg corresponding to parameter
//str with chars in reverse order.

string removePunct(const string& src, const string& punct);
//returns copy of string src with chars in string punct removed

string makeLower(const string& s);
//returns a copy of parameter s that has all upper case
// chars forced to lower case, other chars unchanged.
//Uses <cctype>, for the predefined function tolower

bool isPal(const string& thisString);
//uses makeLower, removePunct.
//if thisString is a palindrome,
// return true;
//else
// return false;

int main( )
{
    string str;
    cout << "Enter a candidate for palindrome test "
            << "\nfollowed by pressing return.\n";
    getline(cin, str);
    if (isPal(str))
        cout << "\"" << str + "\" is a palindrome ";
    else
            cout << "\"" << str + "\" is not a palindrome ";
    cout << endl;
    return 0;
}

void swap(char& lhs, char& rhs)
{
    char tmp = lhs;
    lhs = rhs;
    rhs = tmp;
}

string reverse(const string& str)
{
    int start = 0;
    int end = str.length( );
    string tmp(str);

    while (start < end)
    {
        end--;
        swap(tmp[start], tmp[end]);
        start++;
    }
    return tmp;
}

//returns arg with all upper case chars forced to lower case,
//other chars unchanged. Function makeLower uses cctype,
//for the predefined function tolower.
string makeLower(const string& s)
{
    string temp(s); //This creates a working copy of s
    for (int i = 0; i < s.length( ); i++)
        temp[i] = tolower(s[i]);
    return temp;
}

//returns a copy of src with chars in punct removed
string removePunct(const string& src, const string& punct)
{
    string no_punct;
    int src_len = src.length( );
    int punct_len = punct.length( );
    for (int i = 0; i < src_len; i++)
    {
        string aChar = src.substr(i,1);   // or use src.at(i);
        int location = punct.find(aChar, 0);   // can omit 2nd arg.
        //find location of successive chars of src in punct

        if (location < 0 || location >= punct_len)
            no_punct = no_punct + aChar; //keep if not in punct
    }
    return no_punct;
}

//uses functions makeLower, removePunct.
//Returned value:
//if this_String is a palindrome,
// return true;
//else
// return false;
bool isPal(const string& this_String)
{
    string punctuation(",;:.?!'\" "); //includes a blank
    string str(this_String);
    str = makeLower(str);
    string lowerStr = removePunct(str, punctuation);
    // more efficient if we switch the above 2 lines (why?)
    return lowerStr == reverse(lowerStr);
}