hi
can anyone help me
i have the code that checks the word entered a palindrome or not
it works in visual c++ but when i run the same in unix it is giving me errors can anyone tell me how to run it unix plz.....
#include <iostream>
#include <string> // This standard library file contains string functions
#include <cstdlib>
using namespace std;

int main( )
{
char str1[ 20 ] ,str2[20];
// int strrev,getchar;
cout<< "Enter String to be checked for Palindrome : \n";
cin>>str1 ;
strcpy(str2,str1); // Firstly copy original string ie. str1 into second string ie. str2

strrev(str2); // Now Reverse the second string ie. str2

if( strcmp(str1,str2) == 0 ) //Now compare oroginal string str1 with reversed string ie. str2

cout<< "String is Palindrome" ; // If str1 and str2 are same then the string is Palindrome
else
cout<< "String is not Palindrome" ; // otherwise string is not Palindrome

getchar();
}

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

[search]palindrome c++[/search]

Check what strrev(str2); is. Is is a standard function (might be)

And don't you need the header #include <cstring instead of #include <string> etc

The strrev() function is somewhat more common on windows. In linux you have an even shot of having it and not having it. It isn't in the ISO standard (AFAIK).

Here's a simple strrev:

char *strrev( char *s ) {
  char *u, *v;
  char c;
  for (u = s, v = s +strlen( s ) -1; u < v; ++u, --v) {
    c = *u;
    *u = *v;
    *v = c;
    }
  return s;
  }

You could combine the strcpy() and strrev() functions into one function to reverse and copy a string at once...

Also, iamthwee makes a good point. Make sure you are compiling with -Wall to see all errors you can get. Windows compilers are notorious for letting you get away with improper headers.

All these palindrome programs --isn't this too strict? After all "A man, a plan, a canal: Panama!" is a palindrome, but none of the programs posted so far considers ignoring punctuation and spaces. Ask your professor "Can I attain a 'C'?".

commented: I like the last palindrome :) +11

hey this is what i got and it working perfect..

#include <stdio.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

void main ()
{

char string[80];
int isPalindrome = TRUE;
int i,j;


printf("Enter a word: ");
gets(string);
j = strlen(string) - 1;
i = 0;


while(i <= j && isPalindrome) {

if(string != string[j]) {
isPalindrome = FALSE;
}
i++;
j--;
}

if(isPalindrome) {
printf("%s is a palindrome!\n", string);
}
else {
printf("%s is not a palindrome\n", string);
}
}

oh sorry this was the wrong post

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.