i have an assignment for class where i have to use pointers to check if an input is a palindrome. i cant use [] or indexes in my bool function
below is what i have so far but the issue is no matter what i input it says its a palindrome, i did i debug and its not going to the bool function but i dont know why
#include <iostream>
#include <cstring>
usingnamespace std;
bool isAPalindrome(char* palindrome);
int main()
{
char palindrome[30];
bool palindrome_check;
cout << "Please enter an word or phrase.\n";
cin.getline(palindrome, 30);
palindrome_check = isAPalindrome(palindrome);
if (palindrome_check = true)
{
cout << "Input is a palindrome\n";
}
else
{
cout << "Inputis not a palindrome\n;";
}
system("pause");
return 0;
}
bool isAPalindrome(char* palindrome)
{
char* front;
char* rear;
front = palindrome;// starts at the left side of the c string
rear = (palindrome + strlen(palindrome)) - 1;//starts at the right side of the c-string. adds the c string plus the incriment value of s
while (front <= rear)
{
if (front = rear)
{
front++;
rear--;
}
else
{
returnfalse;
}
}
returntrue;
}
this is not a wrong statement but it behaves different to your intention. What this basically does is:
1 2
palindrome_check = true; //Assigns the value 'true' to the variable, overriding the previous value
if(palindrome_check) //Checks if palindrome_check is logically true, which it is because you assigned true it at the previous line
There is also a mistake in your isPalindrome function:
1 2 3 4 5 6
if( front = rear )
//This basically equals to:
front = rear; //assigns the address of the pointer rear to the pointer front
if( front == true) //Checks if front is logically true (any value greater than zero is logically true) since front is a valid pointer it is greater than zero (not a NULL pointer) and therefore logically true.
You should use the following:
if ( (*front) == (*rear) )
Here you are dereferencing both pointers and checking if the chars they are pointing to are equal
Thank you vlad from moscow and Machtl the code is actually working now
another question i have is: if i input madam im adam it says that it is not a palindrome when it should say its a palindrome, what would i have it add to fix this?
what if i was to delete all the white spaces between the words and then run it through the palindrome test.
i tried to do cin.get and isalpha but those didnt work.
and ideas?
well i was trying to think of way to get it to skip over the spaces but i couldnt think of anything to do that, any hints on which part of my code id have to modify to make it skip over the spaces?
bool isAPalindrome(char* palindrome)
{
char* front = palindrome;// starts at the left side of the c string
char* rear = (palindrome + strlen(palindrome)) - 1;//starts at the right side of the c-string. adds the c string plus the incriment value of s
while (front <= rear)
{
if (*front == *rear)
{
do {
front++;
} while(' ' == *front);
do {
rear--;
} while(' ' == *rear);
}
else
{
returnfalse;
}
}
returntrue;
}
Your function is wrong. For example consider string "AA "; where the last symbol is blank. Your function will return false though this string is a palindrome.
You are right. However, personally I like to implement checks like first != last before actually using the function instead of putting that inside the function. The reason is overhead: if I know the size of the container > 0, then I do not want to check anything.
1 2 3 4 5 6 7
int main()
{
// ... some containers declared here, unsure if size > 0
if (!container.empty())
palindrome(container.begin(), container.end();
return 0;
}
int main()
{
char myStr[100];
int n;
cout<<"enter the length of the string \n";
cin>>n;
cout<<"enter the string \n";
for(int i=0;i<n;i++)
{
cin>>myStr[i];
}
The problem with the "rear" loop is that it cannot handle strings which just contain spaces. The "front" loop with stop at null terminator at end of string, but there's no "null start" to help in the "rear" case.
On top of that, you really want the function to return false for a blank string. If both front and rear move to the other end of a blank string, then the while loop will not run, and the function will return true.
So you need to do something a little bit more involved.
bool isAPalindrome(char* palindrome)
{
// if palindrome is NULL, terminate immediately. Dereferencing
// a null pointer will blow up the program.
if(NULL == palindrome)
returnfalse;
// find the first non-whitespace char (now we know we have some chars)
char* front = palindrome;
while(isspace(*front))
{
++front;
}
// find out how much longer the string is, from the first non-
// non-whitespace char. If the answer's zero, then we were passed
// a string containing only whitespace
size_t len = strlen(front);
if(0 == len)
returnfalse;
// find the last non-whitespace char
char* rear = front + len - 1;
while(isspace(*rear))
{
--rear;
}
while (front <= rear)
{
#if 0 // enable for debugging
cerr << *front << " - " << *rear << endl;
#endif
if ((*front) != (*rear))
returnfalse;
do
{
++front;
} while(isspace(*front));
do
{
--rear;
} while(isspace(*rear));
}
returntrue;
}