A program that is to check if a string is a palindrome.
Every character must be put to lowercase.
so if anyone wants to write some code, then by all means go ahead
A program that is to check if a string is a palindrome.
Every character must be put to lowercase.
so if anyone wants to write some code, then by all means go ahead
Sorry, but are that words that are the same when reversed?
Write your code, then come back if you have specific problems with it, (+OS and compiler). If you want people to do your homework for you, then you'll get flamed.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
[HINT]
No offense or anything, but a search of the board may help. [/HINT]
Try:
bool palindrome = true; // Assume palindrome from start and test for non palindrome.
for (i = 0; i < strlen (s) / 2; i++) // iterate first half of string.
{
if (s[i] != s[strlen (s) - i - 1])
{
palindrome = false;
break;
}
}
if (palindrome)
printf ("YAY!!!!!");
else
printf ("NO!");
that's a bit of overkill dont you think?? how many times is strlen going to get called if the string is really big?? and that's not a case insensitive solution
Nicholas35: i've already posted code to solve this problem in another thread.... before jumping on and asking people to do the code for you... have a search through the web, and the previous posts.. and you'll be amazed at what you find.
Having said that.. since i'm in a good mood.. here's the code:
Code:int Palindrome(char* text) { char* start = text; char* end = text + strlen(text) - 1; while(end > start) { if(tolower(*start) != tolower(*end)) { return(0); } end--; start++; } return(1); } if(Palindrome(string)) { cout << "This is a palindrome\n"; }
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.
Yes, I hear you. That was just a quick and cheap solution (and it was late). Add the line:
int stringLength;
stringLength = strlen (s) / 2;
And use that in the for loop.
Hey it's cool... i wasn't *****ing!
strlen (s) / 2
this isn't the line that's a problem.. the compiler will probably optimise that for you... the line that was the problem is :
[i]if (s != s[strlen (s) - i - 1])
this is the call that needs to be replaced
Just simply making a point that there are better ways to do things than the obvious.
i reckon there's 99.9% chance that there's a better solution than the one i wrote too!
U.
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.