• Welcome to Valhalla Legends Archive.
 

Palindrome Detection

Started by AC_Drkan, May 10, 2004, 10:25 AM

Previous topic - Next topic

AC_Drkan


#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
"The Arguments of Today Result in the Wars of Tomorrow" - Quote By Muah.
<@Logan> I spent a minute looking at my own code by accident.
<@Logan> I was thinking "What the hell is this guy doing?"

<kow`> "There are 10 types of people in the world... those who understand binary and those who don't."
<SpaceRain> That's only 2 types of people, kow.
<SpaceRain> STUPID


<[TN]FBMachine> i got kicked out of barnes and noble once for moving all the bibles into the fiction section

God i love Bash.org.

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Eli_1

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

Adron

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

Arta

eew @ doing people's homework :P

iago

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
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Eibro

Much less efficient, but much clearer is
bool palindrome( const string& in ) {
   string inrev = in;
   reverse( inrev.begin(), inrev.end() );
   return inrev == in;
}
Eibro of Yeti Lovers.

Mephisto

ew @ calling main() without a return type!

iago

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


AC_Drkan

#9
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.
"The Arguments of Today Result in the Wars of Tomorrow" - Quote By Muah.
<@Logan> I spent a minute looking at my own code by accident.
<@Logan> I was thinking "What the hell is this guy doing?"

<kow`> "There are 10 types of people in the world... those who understand binary and those who don't."
<SpaceRain> That's only 2 types of people, kow.
<SpaceRain> STUPID


<[TN]FBMachine> i got kicked out of barnes and noble once for moving all the bibles into the fiction section

God i love Bash.org.

Adron

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