954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

palindrome problem

A palindrome is a word that reads the same both forwards and backwards. Examples: anna, nitalarbralatin, amanaplanacanalpanama. Write a function that takes a string as parameter and returns true if the word is a palindrome, false otherwise. Also write a program palindrome.cc that reads words from the terminal and checks whether they are palindromes.

corolaron
Newbie Poster
1 post since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

Try searching the forum.

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

why should I write such a program?

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Take the string and spell it in reverse. Compare the two strings, if they match you got a palindrome. Now show us some code!

vegaseat
DaniWeb's Hypocrite
Moderator
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

try this logic...
You know a word isn't a palindrome if it's an odd number of characters. For even numbered words, use 2 iterators on your string, start one at the beginning, and one at the end. Moving the two iterators towards each other and comparing the values at each position, until their positions in the string cross.
use begin(), end(), and compare()

joshuatree
Newbie Poster
7 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 
try this logic... You know a word isn't a palindrome if it's an odd number of characters.

"dad" is a palindrome.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
"dad" is a palindrome.

depends on the definition used. Some might say it's not a palindrome because the a isn't the same as another letter in the word (even though being of course identical to itself).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

this should work...

#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
const int NULA = 0;
string a, b;
char buff[1026];

int main (void) {
  while( scanf( "\n%s", buff ) == 1 ) {
    b = a = buff;
    reverse( b.begin(), b.end() ); 
    if( a == b ) printf( "PALINDROM\n" );
    else printf( "NOT A PALINDROM\n" );
  }
  return NULA;
}
brahle
Newbie Poster
18 posts since Mar 2006
Reputation Points: 11
Solved Threads: 0
 

this should work...

#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
const int NULA = 0;
string a, b;
char buff[1026];

int main (void) {
  while( scanf( "\n%s", buff ) == 1 ) {
    b = a = buff;
    reverse( b.begin(), b.end() ); 
    if( a == b ) printf( "PALINDROM\n" );
    else printf( "NOT A PALINDROM\n" );
  }
  return NULA;
}

1. Don't be foolish enough to do someone's homeworkand at the very least keep it consistent to one language.

2.If you feel inclined to do ppl's homework then by all means do so but don't bother posting it! That way you're happy, I'm happy everyone is happy. :rolleyes:

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

1. Don't be foolish enough to do someone's homework and at the very least keep it consistent to one language.

2.If you feel inclined to do ppl's homework then by all means do so but don't bother posting it! That way you're happy, I'm happy everyone is happy. :rolleyes:

1. It is consistent to the competition coding, and i don't know to code in other way. I always use things that are proved to be faster or much much easeier to code.
2. Okay i won't do that any more.

brahle
Newbie Poster
18 posts since Mar 2006
Reputation Points: 11
Solved Threads: 0
 

well, if I were a teacher having to grade that I'd fail it immediately anyway.
Ugliest mix of C and C++ I've ever seen, global variables, very bad variable naming, etc. etc.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
well, if I were a teacher having to grade that I'd fail it immediately anyway. Ugliest mix of C and C++ I've ever seen, global variables, very bad variable naming, etc. etc.


Looks like our friend brahle uses cstdio rather than iostream because of the much smaller executable it produces. Maybe it is bad habit to mix C and C++, but then C++ is tolerant of C. I consider that one strength of C++ anytime. NULA (must be Croation for NULL) and return NULA is optional with C++ however.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

Doesn't matter if it's tolerant of old style code, it's there for backwards compatibility only.

I can also run a 16 bit application on XP, doesn't mean I should make them just because they're easier to write...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

#include
#include
main(){
char s[50],t[50];
char *p,*q,*r;
int i=0,j=0;
scanf("%s",s);
p=s+strlen(s)-1;
r=p;
while(i

gkmishr1
Newbie Poster
1 post since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You