Tags:
create new tag
view all tags

Palindrome

//DISPLAY 8.8 Palindrome Testing Program 
//Test for palindrome property.
#include <iostream>
//#include <string>
//#include <cctype>
using namespace std;
 
void swap(char& v1, char& v2);
//Interchanges the values of v1 and v2.
 
string reverse(const string& s);
//Returns a copy of s but with characters in reverse order.
 
string remove_punct(const string& s, const string& punct);
//Returns a copy of s with any occurrences of characters
//in the string punct removed.
 
string make_lower(const string& s);
//Returns a copy of s that has all uppercase
//characters changed to lowercase, other characters unchanged.
 
bool is_pal(const string& s);
//Returns true if s is a palindrome, false otherwise.
 
int main( )
{
    string str;
    cout << "Enter a candidate for palindrome test\n"
         << "followed by pressing Return.\n";
    getline(cin, str);
 
    if (is_pal(str))
         cout << "\"" << str + "\" is a palindrome.";
    else
         cout << "\"" << str + "\" is not a palindrome.";
    cout << endl;
 
    return 0;
}
 
void swap(char& v1, char& v2)
{
    char temp = v1;
    v1 = v2;
    v2 = temp;
}
string reverse(const string& s)
{
    int start = 0;
    int end = s.length( );
    string temp(s);
 
    while (start < end)
    {
       end--;
       swap(temp[start], temp[end]);
       start++;
    }
 
    return temp;
}
 
//Uses <cctype> and <string>
string make_lower(const string& s)
{
    string temp(s); 
    for (int i = 0; i < s.length( ); i++)
        temp[i] = tolower(s[i]);
 
    return temp;
}
 
string remove_punct(const string& s, const string& punct)
{
    string no_punct; //initialized to empty string
    int s_length = s.length( );
    int punct_length = punct.length( );
 
    for (int i = 0; i < s_length; i++)
    {
         string a_char = s.substr(i,1); //A one-character string
         int location = punct.find(a_char, 0);
         //Find location of successive characters
         //of src in punct, starting from the beginning
 
      if (location < 0 || location >= punct_length)
         no_punct = no_punct + a_char; //a_char not in punct, so keep it
    }
 
    return no_punct;
}
//uses functions make_lower, remove_punct
bool is_pal(const string& s)
{
    string punct(",;:.?!'\" "); //includes a blank
    string str(s);
    str = make_lower(str);
    string lower_str = remove_punct(str, punct);
 
    return (lower_str == reverse(lower_str)); //Note that one could use more complicated if-else statement here
}
 

Vectors

Using a Vector

//DISPLAY 8.9 Using a Vector
#include <iostream>
#include <vector>
using namespace std;

int main( )
{
    vector<int> v;
    cout << "Enter a list of positive numbers.\n"
         << "Place a negative number at the end.\n";

    int next;
    cin >> next;
    while (next > 0)
    {
        v.push_back(next);
        cout << next << " added. ";
        cout << "v.size( ) = " << v.size( ) << endl;
        cin >> next;
    }

    cout << "You entered:\n";
    for (unsigned int i = 0; i < v.size( ); i++)
        cout << v[i] << " ";
    cout << endl;

    return 0;
}

 


c

Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2018-04-03 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback