سؤال

right now I'm working on a program which tells you if the string you entered is a palindrome or not, and I am stuck on the last step. There are three essential functions which remove all spaces in a sentence, drop them to lower case, and reverse them, then that's when my function verify's them then returns a boolean value. So right now in this function, Happy Birthday! will come out as yadhtribyppah. This is what I have in my function so far:

string updated1;
string updated2;

updated1 = makeLower(verify);

updated2 = removeNonLetters(updated1);

updated1 = reverse(updated2);


for (int i = 0; i < updated2.length(); i++)
{
    if (updated2[i] != updated1[i])
    {
        break;
        return false;
    }
    else if (updated2[i] == updated1[i])
    {
        return true;

    }



}

}

هل كانت مفيدة؟

المحلول

Assuming there are no errors in your other functions, change your for loop to:

for (int i = 0; i < updated2.length(); i++)
{
    // If we find even one inequality, we know its not a palindrome.
    if (updated2[i] != updated1[i])
        return false;
}
// if the for loop has been executed satisfactorily, we know that it is a palindrome.
return true;

نصائح أخرى

  1. Remove the break and instead just return false in case the characters differ.
  2. Don't return true inside the loop - You don't know it's a palindrome until you have gone through all the characters! Instead, just return true if the for loop finishes, outside of the loop.

The reason this isn't working is because you have a break statement before your return statements. This breaks out of the loop, and thus the return never gets called.

Possible Solutions:

1) Remove the break statement in the "if not equal" case. 2) Return false by default outside of the loop at the end of the function.

EDIT:

I also just noticed that you're returning from inside the "if is equal" case. If you do this, then it's only going to check the first set of characters. A better algorithm would be to return false in the "if not equal" case, remove the "if equal" case, and then return true by default at the end of the function.

You shouldn't have that break before the return statement in the first if and also shouldn't return true in the second because you don't finish the loop.

You can do the whole thing like this:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isnotalpha(char c){return !isalpha(c);}

int main(void) 
{
    string updated1="AbbA",updated2;
    transform(updated1.begin(),updated1.end(),updated1.begin(),tolower);
    updated1.erase(remove_if(updated1.begin(),updated1.end(),isnotalpha),updated1.end());
    updated2=updated1;
    reverse(updated2.begin(),updated2.end());

    bool palindrome=true;
    for(int i=0; i<updated1.length(); i++)
    {
        if(updated1[i]!=updated2[i]) palindrome=false;
        //if(updated1[i]!=updated1[updated1.length()-1-i]) palindrome=false; Can be done without using updated2
    }
    if(palindrome) cout << "Palindrome!" << endl;
    else cout << "Not Palindrome!" << endl;

    return 0;
}

You don't have to reverse the string just need to check if it is symmetric

bool testIt(const string value)
{ 
  string updated2 = makeLower(removeNonLetters(value));
  int L = updated2.length();
  for(var i = 0; i < L/2; i++)
  {
    if (updated2[i] != updated2[L-1-i])
      return false;
  }
  return true;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top