C++ check if Palindrome Just did this simple application to check whether a string is a palindrome or not

#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

void main()
{
  string str;
  cout &lt;&lt; "Enter a string: "; getline(cin, str);
  cout &lt;&lt; "Your string is: "; cout &lt;&lt; str &lt;&lt; endl;
  cout &lt;&lt; "The reverse of the string is: ";
  int counter = 0;
  for (int reverseIdx = str.size() - 1, forwardIdx = 0; reverseIdx &gt;= 0 &amp;&amp; 
  forwardIdx &lt; str.size(); reverseIdx--, forwardIdx++)
  {
    cout &lt;&lt; str[reverseIdx];
    if (str[reverseIdx] == str[forwardIdx])
    {
      counter++;
    }
  }
  cout &lt;&lt; endl;
  if (counter == str.size())
  {
    cout &lt;&lt; "The string is a Palindrome!" &lt;&lt; endl;
  }
  else
  {
    cout &lt;&lt; "The string is not a Palindrome." &lt;&lt; endl;
  }
}


I'm getting a string from the user, then printing out the same string (I know it's not needed in such small programs). Then using a for loop to check if every character when reversed is equal to the same character when read normally.  

Then using a counter variable I am checking if it's equal to the string size, if it's then it's palindrome.
Advice 1: getting the job done the C++ way

You can check whether the input string is a palindrome in one line:

bool is_palindrome(std::string&amp; text) {
    return std::equal(text.cbegin(), text.cend(), text.rbegin());
}


For Incomputable has spoken: You can make the above run in the worst case faster by the factor of two:

bool is_palindrome(std::string&amp; text) {
    return std::equal(text.cbegin(),
                      std::prev(text.cend(), text.length() / 2),
                      text.rbegin());
}


For Deduplicator has spoken: In is_palindrome, change std::string&amp; to std::string const&amp;; this will guarantee that the input string is not modified.

Advice 2

Whatever you implementation of the palindrome checker is, have a dedicated method that does not output the result to std::cout, but rather returns a boolean indicating whether the input string is a palindrome or not.

Advice 3

void main() does not compile on modern Xcode; change to int main().

Advice 4

Please avoid

using namespace std;


since it pollutes your namespace by importing quite a lot of identifiers/type names.

Advice 5

cout &lt;&lt; "Your string is: "; cout &lt;&lt; str &lt;&lt; endl;


You can write:

cout &lt;&lt; "Your string is: " &lt;&lt; str &lt;&lt; endl;


Alternative implementation

I had this in mind:

#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

bool is_palindrome(std::string&amp; text) {
    return std::equal(text.cbegin(), text.cend(), text.rbegin());
}

using std::cin;
using std::cout;
using std::getline;
using std::string;

int main()
{
    string str;
    cout &lt;&lt; "Enter a string: ";
    getline(cin, str);
    cout &lt;&lt; "Your string is: " &lt;&lt; str &lt;&lt; "\n";
    cout &lt;&lt; "Palindrome: " &lt;&lt; std::boolalpha &lt;&lt; is_palindrome(str) &lt;&lt; "\n";
}