This is what JLBorges meant by:
Yes. But would you be able to submit it? |
You need to learn the basics before you can optimize code... JL has used in built functions from various header files to make his code work.. While learning basic, you need to figure out how these functions work, and how you can manually write them.. People here are ready to help you out, why try and get your work done from them?
Just out of curiosity, if the return type of your function is bool, shouldn't it return true/false instead of 0/1? Also, in your for loop, why check till the end of the loop? Even if you check till the halfway mark (x/2 for even number of letters, and (x-1)/2 for odd), doesn't it mean the rest half is same? It'll increase the efficiency of your code..
Secondly, your code will give the following output for a sample word "hasbah"
palindrome
palindrome
not a palindrome
not a palindrome
palindrome
palindrome |
That's clearly not what you want, do you? This is not a compiler error, but a logical error. Fix it first.. Try something like:
1 2 3 4 5 6 7 8 9 10 11 12
|
bool isPalindrome(string eachWord)
{
int x = strlen(eachWord)-1;
bool isPrime=true;
for(int i = 0; i <= x; i++)
{
if (eachWord[i] != eachWord[x-i]) {
isPrime=false;
}
}
return isPrime;
}
| |
Can't figure about the const char thing.. Maybe someone else can help you with that..