Question

I am trying to determine if my code is a palindrome so I created a reverse function and then a palindrome function. I am trying to assign the reversed Character array into the new function but I can't seem to get it to compile.... any tips? Here is my palindrome function

bool Palindrome(char Characters[], unsigned long length)
{
   char tempstring[62];
   tempstring[62] == reverse(Characters);

   for(int i=0; i <= length; i++){
      if(Characters[i] == tempstring[i])
          return false;
      else
          return true;
   }

}

Here is my reverse function

void reverse(char Characters[], unsigned long length)
{
    char temp;   

    for(int i=0; i<length/2; i++){
        temp = Characters[i];
        Characters[i]=Characters[length-i-1];
        Characters[length-i-1]=temp;

    }
}
Was it helpful?

Solution 2

You are making this quite complicated.

Just find the end of the string (strlen). Read from both ends a character at a time and if they do not match then it is not a palindrome. If the indexes become the same or they cross then you are done. It is indeed a palindrome.

I.e

bool Palindrome(char *s) {
   int left = 0, right = strlen(s) - 1;
   while (left < right) {
      if (s[left] != s[right] return false;
      ++left;
      --right;
   }
   return true;
}

EDIT

Similar vain to construct the reverse

char *Reverse(char *s)
{
    char *rev = new char[strlen(s) + 1];
    int left = 0, right = strlen(s) - 1;
    while (right > -1) {
       rev[left] = s[right];
       right--;
       left++;
    }
    rev[left] = 0;
    // Remember to use delete[]
    return rev;
 }

EDIT 2

Or

void Reverse(char[] s, int len) {
   int left = 0; right = len;
   while (right > -1) {
      char t = s[left];
      s[left] = s[right];
      s[right] = t;
      left++; right--;
   }
}

Then make a copy of the string, reverse it and compare it.

OTHER TIPS

First things first, you have a typo; == is a compare equality, =. You ought to have written

tempstring[62] = reverse(Characters);

But this will still not work. For starters, reverse is a void function and so therefore it does not return a value.

The quickest fix will be to replace that line with

reverse(Characters, length);

(Note that I'm also passing the length parameter as required).

One final thing: if you have organised your file so that reverse appears after Palindrome, then you need to forward declare reverse using this statement:

void reverse(char Characters[], unsigned long length);

That fixes the compilation errors. I defer to you to check the runtime behaviour.

Your error is the line tempstring[62] == reverse(Characters);. You don't need the double = sign. In the future, it would be helpful to post the error messages you get when compiling.

bool Palindrome(char Characters[], unsigned long length)
{
   char tempstring[62];
   tempstring[62] = reverse(Characters);

   for(int i=0; i <= length; i++){
      if(Characters[i] == tempstring[i])
          return false;
      else
          return true;
   }

}

Your error is here:

tempstring[62] == reverse(Characters);

You've wrote == means, a condition that returns true or false (for example: if (5 == 7) -> false)

But what you've actually wanted to do was tempstring[62] = reverse(Characters);

One = means equal (int a = 3)

Two == means to check a condition (for example if (a == b) (and thats why you dont write in ifs: if(a = 3) because it will assign a = 3 and allways get inside the if

First of all, your reverse function returns nothing, so attempting to assign its return value to anything is not going to work:

tempstring[62] == reverse(Characters); // won't work as it is attempting to compare a void
tempstring[62] = reverse(Characters); // won't work as it is attempting to assign a void

From a more fundamental level, testing for a palindrome is much less complex than you are making it:

bool Palindrome(char Characters[], unsigned long length)
{
    bool result = true;
    for(int i=0; i < length / 2; i++)
    {
        if (Characters[i] != Characters[length - i - 1])
        {
            result = false;
            break;
        }
    }
    return result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top