Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

palindrome testing

Status
Not open for further replies.

INDO

Programmer
Mar 18, 2001
12
US
Could somebody please give me some pointers on my code for palindrome testing. As you can see I am new to Borland programming. I think i have the main points okay but there are still errors.

Many thanks.

int main (int argc int, chr** argv)
{
AnsiString word;
int Index;
int Reverse;
char first;
char Last;
word = ReadStringPr("Enter a word: ");
Index = 1;
Reverse = 1;
First = Length(word) + 1;
Last = Length(word) - 1;
while (Index <= Length(word))
{
if(First == Last)
Index = Index + 1;
Reverse = Reverse - 1;
}
WriteStringPr(&quot;The word is a palindrome.&quot;);
{
else
WriteStringPr(&quot;The word is not a palindrome&quot;);
}

getchar();
return 0;
}
 
This should work:

...
char *first, * last;
first = word; // points to the first char
last = word + length(word) - 1; // points to the last char

int counter = length(word) / 2;
bool status = true;
while (counter > 0 )
{
if ( (*first) != (*last))
{
bool = false;
break;
}
first++;
last--;
}

...


Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top