Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: AC_Drkan on May 10, 2004, 10:25 AM

Title: Palindrome Detection
Post by: AC_Drkan on May 10, 2004, 10:25 AM

#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");
}



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
Title: Re:Palindrome Detection
Post by: MyndFyre on May 10, 2004, 10:59 AM
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.
Title: Re:Palindrome Detection
Post by: Eli_1 on May 10, 2004, 01:33 PM
Quote
   if (string2 == string3)
  {
     return TRUE;
  }
  if (string2 != string3)
  {
     return FALSE;
  }
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 from: Myndfyre on May 10, 2004, 10:59 AM
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.
Aww Mynd, don't be mean. 90% of people start with lame things like
"Hello, World!\n"
This is just the next step in a chain of lame beginning projects. Gotta crawl before you can walk... Or something like that...
Title: Re:Palindrome Detection
Post by: Adron on May 10, 2004, 02:36 PM
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; }
Title: Re:Palindrome Detection
Post by: Arta on May 10, 2004, 04:14 PM
eew @ doing people's homework :P
Title: Re:Palindrome Detection
Post by: iago on May 10, 2004, 04:29 PM
Quote from: Arta[vL] on May 10, 2004, 04:14 PM
eew @ doing people's homework :P

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
Title: Re:Palindrome Detection
Post by: Eibro on May 10, 2004, 04:56 PM
Much less efficient, but much clearer is
bool palindrome( const string& in ) {
   string inrev = in;
   reverse( inrev.begin(), inrev.end() );
   return inrev == in;
}
Title: Re:Palindrome Detection
Post by: Mephisto on May 10, 2004, 06:14 PM
ew @ calling main() without a return type!
Title: Re:Palindrome Detection
Post by: iago on May 10, 2004, 06:53 PM
Quote from: Mephisto on May 10, 2004, 06:14 PM
ew @ calling main() without a return type!

There are many technical and logical problems with his code, which is why I recommended he seek professional help.
Title: Re:Palindrome Detection
Post by: AC_Drkan on May 11, 2004, 04:29 AM
Quote from: iago on May 10, 2004, 06:53 PM
Quote from: Mephisto on May 10, 2004, 06:14 PM
ew @ calling main() without a return type!

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

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:

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;
}

I use that in my greet bots.
Title: Re:Palindrome Detection
Post by: Adron on May 11, 2004, 03:11 PM
Quote from: Arta[vL] on May 10, 2004, 04:14 PM
eew @ doing people's homework :P

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...