Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3661646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:19:42+00:00 2026-05-19T01:19:42+00:00

I wrote a function to check whether a word is palindrome or not but

  • 0

I wrote a function to check whether a word is palindrome or not but “unexpectedly”, that function failed quite badly, here it is:

bool isPalindrome (const string& s){
 string reverse = "";
 string original = s;
 for (string_sz i = 0; i != original.size(); ++i){
  reverse += original.back();
  original.pop_back();
 }

 if (reverse == original)
  return true;
 else
  return false;
}

It gives me “string iterator offset out of range error” when you pass in a string with only one character and returns true even if we pass in an empty string (although I know its because of the intialisation of the reverse variable) and also when you pass in an unassigned string for example:

string input;
isPalindrome(input);

Later, I found a better function which works as you would expect:

bool found(const string& s)
{
 bool found = true;
 for (string::const_iterator i = s.begin(), j = s.end() - 1; i < j; ++i, --j) {
  if (*i != *j)
   found = false;
 }
 return found;
}

Unlike the first function, this function correctly fails when you give it an unassigned string variable or an empty string and works for single characters and such…

So, good people of stackoverflow please point out to me why the first function is so bad…

Thank You.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:19:43+00:00Added an answer on May 19, 2026 at 1:19 am
    for (string_sz i = 0; i != original.size(); ++i) {
        reverse += original.back();
        original.pop_back();
    }
    

    original.size() changes as you pop elements off the back. Effectively, you keep incrementing i and decrementing original.size(); they may never be equal.

    if (reverse == original)
    

    This will never be true since you’ve just removed all of the elements from original and added them in reverse order to reverse. original will always be empty at this point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a function to check whether a number is prime or not: prime
This is a recursive function I wrote to determine whether or not a given
I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .
I have this Array i wrote a function MostFreq that takes an array of
I wrote a while loop in a function, but don't know how to stop
I wrote a managed C++ class that has the following function: void EndPointsMappingWrapper::GetLastError(char* strErrorMessage)
I wrote a sample program at http://codepad.org/ko8vVCDF that uses a template function. How do
I've wrote some code that will check two dates - they are split into
I have written a function, which has to check whether a username has been
Recently I wrote a function to generate certain sequences with nontrivial constraints. The problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.