Valhalla Legends Forums Archive | C/C++ Programming | Palindrome Detection

AuthorMessageTime
AC_Drkan
[code]
#include <stdio.h>
#include <string.h>
#define MAXLENGTH   100
#define TRUE         1
#define FALSE      0

int tester (char string1[MAXLENGTH])
{
   char string2[MAXLENGTH];
   char string3[MAXLENGTH];
   int i;
   
   for ( i = 0; i >= strlen(string1); ++i)
   {
      string3[ i ] = string1[ i ];
   }
   
   for ( i = strlen(string1); i <= 0; --i)
   {
      string2[ i ] = string1[ i ];
   }
   
   printf ("PASSED LOOP");
   
   if (string2 == string3)
   {
      return TRUE;
   }
   if (string2 != string3)
   {
      return FALSE;
   }
   else
   {
      printf ("Didn't understand the input. Exiting...\n");
   }
   
}

main ()
{
   char enter[MAXLENGTH];
   int result;

   printf ("Enter a Word: ");
   scanf("%s", enter);
   
   result = tester (enter);
   
   if ( result == TRUE )
      printf ("\nYES %s is a Palindrome\n", enter);
   else if ( result == FALSE)
      printf ("\nNO %s isn't a Palindrome\n", enter);
   else
      printf ("\nDidn't understand the input. Exiting......\n");
}

[/code]

ok
this is ther code im using to find out if words entered to string 1 are a palindrome.
radar is a palindrome.
mom is a palindrome.

Basically a palindrome is a word that can be spelled backwards and forwards and still be the same word

Please help
this project is due this friday
May 10, 2004, 3:25 PM
Myndfyr
Cmon man, a palindrome test is done in basic CS classes. Don't be a lamer. :P

It's a for loop that compares the final index to the first index, incrementing the first index and decrementing the last index. When they are within 2 of each other, you are done (the string is a palindrome), or if they are not and they don't match, you break out of the loop, and it is not a palindrome.
May 10, 2004, 3:59 PM
Eli_1
[quote]
if (string2 == string3)
{
return TRUE;
}
if (string2 != string3)
{
return FALSE;
}
[/quote]
I'm a newbie to C++, but wouldn't that raise an error. I didn't think you could compare character arrays like that.

strcmp?


[quote author=Myndfyre link=board=30;threadid=6735;start=0#msg59352 date=1084204792]
Cmon man, a palindrome test is done in basic CS classes. Don't be a lamer. :P

It's a for loop that compares the final index to the first index, incrementing the first index and decrementing the last index. When they are within 2 of each other, you are done (the string is a palindrome), or if they are not and they don't match, you break out of the loop, and it is not a palindrome.
[/quote]
Aww Mynd, don't be mean. 90% of people start with lame things like
[code]"Hello, World!\n"[/code]
This is just the next step in a chain of lame beginning projects. Gotta crawl before you can walk... Or something like that...
May 10, 2004, 6:33 PM
Adron
bool palindrome(const char *str, int len) { return len < 2 || *str == len[str] && palindrome(str + 1, len - 2) ; }


bool palindrome(const char *p) { for(char *q = p + strlen(p) - 1; q > p; q--, p++) if(*p != *q) return false; return true; }
May 10, 2004, 7:36 PM
Arta
eew @ doing people's homework :P
May 10, 2004, 9:14 PM
iago
[quote author=Arta[vL] link=board=30;threadid=6735;start=0#msg59402 date=1084223660]
eew @ doing people's homework :P
[/quote]

Of course, if you turn Adron's code into the majority of C++ classes you wouldn't get many marks anyway.

Based on the code you've given you have no idea what you're doing and should probably ask your teacher for help. Seriously
May 10, 2004, 9:29 PM
Eibro
Much less efficient, but much clearer is
[code]bool palindrome( const string& in ) {
string inrev = in;
reverse( inrev.begin(), inrev.end() );
return inrev == in;
} [/code]
May 10, 2004, 9:56 PM
Mephisto
ew @ calling main() without a return type!
May 10, 2004, 11:14 PM
iago
[quote author=Mephisto link=board=30;threadid=6735;start=0#msg59432 date=1084230842]
ew @ calling main() without a return type!
[/quote]

There are many technical and logical problems with his code, which is why I recommended he seek professional help.
May 10, 2004, 11:53 PM
AC_Drkan
[quote author=iago link=board=30;threadid=6735;start=0#msg59435 date=1084233204]
[quote author=Mephisto link=board=30;threadid=6735;start=0#msg59432 date=1084230842]
ew @ calling main() without a return type!
[/quote]

There are many technical and logical problems with his code, which is why I recommended he seek professional help.
[/quote]

Me?

Cause it runs, but it just returns no for everything i do
and
strcmp(string1,"hi")
would return 0 if string1 is exactly equal to hi

If you want an example here ya go:
[code]
if (stricmp(szSpeaker,"angel_drkan@azeroth") == 0)
{
   Send("/emote . : Greetings Master, Users logged on since my log on: %i, Currently on Number %i : .\r\n", y, x);
   x = x - 1;
   return 1;
}
[/code]
I use that in my greet bots.
May 11, 2004, 9:29 AM
Adron
[quote author=Arta[vL] link=board=30;threadid=6735;start=0#msg59402 date=1084223660]
eew @ doing people's homework :P
[/quote]

I was thinking about whether to post commented pseudo-code/ideas or uncommented c++ code. I chose this because it was more fun. I figure he'll have to understand his answer to be able to get a score for it anyway. This way he'll get to ponder two solutions - which one to pick...
May 11, 2004, 8:11 PM

Search