Palindromes Help

I'm brand new to C++ and I have a little Java experience.
I am testing for palindromes and I want to compare an array to itself inversely. Is there a function that does this already or will I have to write something?

I currently have this, but the second for loop isn't returning the inverse array.
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
#include <iostream>
#include <string>
using namespace std;

int main()
    {
     string UserProvidedWord;
    cout << "Word: ";
    cin >> UserProvidedWord;
    cout << UserProvidedWord << "\n";
    int lengthOfProvidedWord = UserProvidedWord.length();
    string ComparisonArray[100];
    int differential = lengthOfProvidedWord;

    for(int i = 0;i < UserProvidedWord.length();i++)
    {
        ComparisonArray[i] = UserProvidedWord[i];
        for(int y = differential; y < 0; y--)
    {
        if(ComparisonArray[y] == ComparisonArray[i])
        {
            cout << UserProvidedWord << " is a palindrome";
        }
        else
        {
                cout << "This is not a palindrome";
        }
    }

    }

    }

The idea is that by outputting the array values from the highest to lowest (y--), it would print out the last letter until it reached the first letter in the array. The y for loop isn't working for some reason.
Last edited on
> Is there a function that does this already

Yes. std::equal() used in conjunction with the strings reverse iterator.
http://en.cppreference.com/w/cpp/algorithm/equal
http://en.cppreference.com/w/cpp/string/basic_string/rbegin

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
#include <iostream>
#include <cctype>
#include <algorithm>

bool is_palindrome( const std::string& str )
{
    // we want to ignore punctuation, whitespace, capitalization and non-printable chars
    std::string s ;
    for( char c : str ) if( std::isalnum(c) ) s += std::tolower(c) ;

    // we need to check only if the first half of the string is the reverse of the second half
    // std::equal - http://en.cppreference.com/w/cpp/algorithm/equal
    //              note: there is a palindrome example at the end of the page
    // rbegin() yields an iterator that iterates backward from the last character
    // http://en.cppreference.com/w/cpp/string/basic_string/rbegin
    return !s.empty() && std::equal( s.begin(), s.begin()+s.size()/2, s.rbegin() ) ;
}

int main()
{
    const std::string phrases[] =
    {
        "Palindromes often consist of a phrase or sentence",
        "Punctuation, capitalization, and spacing are usually ignored",
        "A man, a plan, a canal - Panama!",
        "Do geese see God?"
    };

    for( const std::string& str : phrases ) if( is_palindrome(str) )
         std::cout << str << '\n' ;
}

http://coliru.stacked-crooked.com/a/bfa296f17a149423
JLBorges:

Why are the parameters in that format without a space between & and str? And what is str?

In line 9, the for loop (car c : str) <--- I have no idea what that means. Why is there only one colon instead of usual (::) two?

Then at line 16...
Return !s.empty ? Aren't you passing false there instead of it actually testing if it's empty?
Shouldn't it be if !s.empty?

Maybe the code is just very loosely written/not easily readable.
Last edited on
> Why are the parameters in that format without a space between & and str?

These are equivalent:
1
2
3
4
bool is_palindrome( const std::string&  str ) ;
bool is_palindrome( const std::string  &str ) ;
bool is_palindrome( const std::string  &  str ) ;
bool is_palindrome( const std::string&str ) ;


> And what is str?

The type of variable str is '(lvalue) reference to const std::string'


> the for loop (car c : str)

See: http://www.stroustrup.com/C++11FAQ.html#for


> Shouldn't it be if !s.empty?

These are equivalent:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 1
return !s.empty() && std::equal( s.begin(), s.begin()+s.size()/2, s.rbegin() ) ;

// 2
if( !s.empty() && std::equal( s.begin(), s.begin()+s.size()/2, s.rbegin() ) ) return true ;
else return false ;

// 3
if( !s.empty() )
{
    if( std::equal( s.begin(), s.begin()+s.size()/2, s.rbegin() ) ) return true ;
}
return false ;

// 4
if( s.empty() ) return false ;
return std::equal( s.begin(), s.begin()+s.size()/2, s.rbegin() ) ;
Last edited on
I know what a for loop is... but is char c : str == char c = str ?
Last edited on
Topic archived. No new replies allowed.