i need to create a palindrome program. in our computer lab, this program works but here at home, it doesn't. help..

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
int main()
{
   clrscr();
   char word[20],rev[20],chr;
   cout<<"This porgram checks if the word is a PALINDROME word.\n";
   do
   {
       cout<<"\nEnter word: ";
       cin>>word;

       for(int i=0,j=strlen(word)-1;i<=j,j>=0;i++,j--)
	     rev[i]=word[j];

       if(strcmp(rev,word)==0)
	  cout<<"The word "<<word<<" is PALINDROME.";
       else
	  cout<<"The word "<<word<<" is NOT PALINDROME.";

       cout<<"\n\nTRY AGAIN? [Y]";
       cin>>chr;
   }
   while(chr=='y'||chr=='Y');
   getch();
   return 0;
}

From what comes to my mind immediately after seeing this, you are in effect copying only about half of the string since you begin by assigning i to 0 and j to the last character of the word, then you increment i while simultaneously decrementing j. i and j will meet somewhere in the middle so that rev only has about half of the characters of word. Now in this case strcmp will never return a 0.
This is IMO, a long time I haven't been doing a lot of C++, so tell me if I am wrong.

As mentioned in my previous post, I have for long lost touch with C++, but I assume from what I know, that none of the current collection of GNU compilers use the .h suffix. The new crop of compilers are bound to throw an error at you for that.

As mentioned in my previous post, I have for long lost touch with C++, but I assume from what I know, that none of the current collection of GNU compilers use the .h suffix. The new crop of compilers are bound to throw an error at you for that.

Yeah, get rid of conio.h altogether, replace string.h and stdio.h with cstring and cstdio . I actually don't know that it matters whether you use stdio.h or cstdio and whether you use cstring or string.h , but it makes it clear that it's a C++ program. Use string. For sure use iostream instead of iostream.h . If you get rid of the conio.h stuff, all you need to include is iostream for this program. And you need to use std::cout and std::cin or namespace std .

This compiled and ran for me using Dev C++.

#include<iostream>
//#include<cstring>
//#include<cstdio>
using namespace std;

int main()
{
   char word[20],rev[20],chr;
   cout<<"This porgram checks if the word is a PALINDROME word.\n";
   do
   {
       cout<<"\nEnter word: ";
       cin>>word;

       for(int i=0,j=strlen(word)-1;i<=j,j>=0;i++,j--)
	     rev[i]=word[j];

       if(strcmp(rev,word)==0)
	  cout<<"The word "<<word<<" is PALINDROME.";
       else
	  cout<<"The word "<<word<<" is NOT PALINDROME.";

       cout<<"\n\nTRY AGAIN? [Y]";
       cin>>chr;
   }
   while(chr=='y'||chr=='Y');
   
   cin.get ();
   return 0;
}

Thr program still has a bug, though. You are not zero-terminating the c-string "rev". The best way is perhaps:

int i, j;
        for(i = 0, j = strlen(word) - 1; j >= 0; i++, j--)
            rev[i] = word[j];
        rev[i] = 0; // zero-terminate rev

Note that I changed i <= j, j >= 0 to j >= 0 since that's all it's evaluating to anyway. (I can't think of any reason to use the comma operator in the test of a for loop.)

i need to create a palindrome program. in our computer lab, this program works but here at home, it doesn't. help..

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
int main()
{
   clrscr();
   char word[20],rev[20],chr;
   cout<<"This porgram checks if the word is a PALINDROME word.\n";
   do
   {
       cout<<"\nEnter word: ";
       cin>>word;

       for(int i=0,j=strlen(word)-1;i<=j,j>=0;i++,j--)
	     rev[i]=word[j];

       if(strcmp(rev,word)==0)
	  cout<<"The word "<<word<<" is PALINDROME.";
       else
	  cout<<"The word "<<word<<" is NOT PALINDROME.";

       cout<<"\n\nTRY AGAIN? [Y]";
       cin>>chr;
   }
   while(chr=='y'||chr=='Y');
   getch();
   return 0;
}

i think u should only include<iostream.h> and else do as suggested by nucleon........

i think u should only include<iostream.h> and else do as suggested by nucleon........

Read the posts written above.

I don't get it. What's the point of duplicating the source array in a reverse order? You can check the beginning and the ending of the array at the same time. And you can still use the array's indexing method with strings to compare each character to another.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string word;
	bool chk = true;

	cout << "Give me a word: ";
	getline( cin, word );

	int n = word.length();

	for( int i = 0, j = n - 1; i < n; i++,j-- )
	{
		if( word[i] != word[j] )
		{
		    chk = false;break;
		}
	}

	if( chk ) cout << word << " is a palindrome!";
	else cout << word << " is not a palindrome!";

	cin.get();
	return 0;
}

This is the same thing, but without copying the source. Only difference this one uses string class, so you won't be limited by the size of your arrays, and also you won't have to bother with memory allocations. :)( if the word is longer than 19 characters )

im witing this program in borland turbo c++, because that is the compiler we use at school. any suggestions about my code?

STL solution:

bool is_palindrome(string const & str)
{
    string temp1(str);
    temp1.erase(remove_if(temp1.begin(), temp1.end(), isspace), temp1.end());
    transform(temp1.begin(), temp1.end(), temp1.begin(), tolower);
    string temp2(temp1);
    reverse(temp2.begin(), temp2.end());
    return temp1 == temp2;
}
This question has already been answered. Start a new discussion instead.