Hello every one. I am new to programming so i am sorry if this is a simple question. I am trying to write a function that will return "True" if the number I hand it is palindromic.
#include <iostream>
#include <string>
#include <cstdlib>
usingnamespace std;
bool palin_test(string str);
int main()
{
string test = "999";
cout << sizeof(test) << " size of String" << endl;
cout << test << endl;
cout << palin_test(test);
}
bool palin_test(string str) {
bool mark = 1; // Set value to return
string newstr; // New string variable
if ((sizeof(str) - 1) <= 1) { // make sure value is greater than one
return mark; // if one, return true
}
if (atoi(str[0]) == atoi(str[(sizeof(str) - 1)])) { //test first digit and last digit to see if equal.
cout << "\ntest";
newstr = str; // remove first and last digit.
newstr.erase(0, 1);
newstr.erase((sizeof(newstr) - 1), 1);
palin_test(newstr); // recursive call of new string
}
cout << str << " New string" << endl;
cout << sizeof(str) << " size of String" << endl;
return (mark = 0);
}
When I try to compile this I keep getting errors at line 32. This code is still a work in progress so I need to polish it up a lot but it seems to me it should work, although obviously it doesn't. :)
P.S sorry about all of the random "cout" statements. I was using them to de-bug.
atoi takes a c-string argument. It does not take a single char. A conversion seems like overkill here, anyway. Just directly compare the first character to the last character. Note that sizeof(std::string) does not give you the length of the string.
Thanks for the response Cire. I tried what you suggested and it worked great. But I am not getting the function to return the result I want. I have been working on it for a few hours and cannot locate the issue. Here is my new code.
#include <iostream>
#include <string>
#include <cstdlib>
usingnamespace std;
bool palin_test(string str);
int main()
{
string test = "99999";
cout << palin_test(test);
}
bool palin_test(string str) {
bool mark = 0; // Set value to return
string newstr; // New string variable
char a = str[0];
char b = str[str.size() - 1];
if (str.size() <= 2 ) { // make sure value is greater than one. "2" is due to null operator.
mark = 1;
cout << " mark is " << mark << endl;
return mark; // if one, return true
}
cout << " String before first if " << str << endl;
cout << " A is " << a << endl << " B is " << b << endl;
if ( a == b ) { //test first digit and last digit to see if equal.
newstr = str; // remove first and last digit.
newstr.erase(0, 1);
newstr.erase(newstr.size() - 1, 1);
cout << " New String size is " << newstr << endl;
palin_test(newstr); // recursive call of new string
}
return mark; // return "0"
}
The number passed to this function should return a value of 1 or true but I keep getting a 0 or false. I think the problem might be in the if statement at line 28 but I can't find it. If any one can point out what I am missing I would very much appreciate it. Thank you
When I did this years ago, I just reversed the string then compared it to the original.
When using booleans, just use true or false (not 0's or 1's)- you won't need the mark variable at all. Technically it doesn't matter whether you use 0 or 1, but the bool variable was invented because true or false are easier - so I prefer to not mix the 2 concepts.