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.

Recommended Answers

All 13 Replies

Try searching the forum.

why should I write such a program?

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

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()

try this logic...
You know a word isn't a palindrome if it's an odd number of characters.

"dad" is a palindrome.

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

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;
}
Member Avatar for iamthwee

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

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.

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.

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

#include<stdio.h>
#include<string.h>
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<strlen(s)){
t=*p;
p--;
i++;}
q=s;
while(j<strlen(s)){
if(*r==*q){
r--;
q++;
j++;}
else{break;}}
if(j==i){
printf("It's A Palendrome.");}
else{printf("It's Not A Palendrome.");}}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.