Working on a small program that will take a string from a user and see if it is a palindrome. (A phrase that is spelled the same way backwards, like “Never odd or even”) I have already built functions that removes spaces and any non-alphabetical characters from the string and then made a copy of the string as well. (All three of these functions have been fully tested, no problems there.) Now I am working on a function that needs to compare both strings to see if the string is spelled the same way forwards and backwards.
The function should decide if it is a palindrome by looping through the main string forwards and the copy backwards and comparing each element. Yet I keep getting a false answer, what am I doing wrong?
_Bool isPalindrome(char str[], char copy[])
{
int i = 0;
int count = 0, j = 0;
// loop through the main string to find the number of elements
while(str[j] != '\0')
{
count++;
j++;
}
//loop through the main string until the null character.
while(str[i] != '\0')
{
// to loop through the copy backwards,
// use the size of the first and subtract i
size = count - i;
if(str[i] != copy[size])
return FALSE;
i++;
}
return TRUE;
}
It should be: