hi again!! I did this question:

Create a class responsible for determining whether a string is a palindrome.

I did the code. but i have some errors that i dont know how to fix.

these are the errors:

1- syntax error before `)' token
2- syntax error before `)' token
3- In function `int main()':
4- ` Ispalindrome' undeclared (first use this function)

#include <iostream>
#include <conio.h>
#include <string.h>
 
using namespace std;
 
 
class palindrome
{
     string s;
 
     public:
 
     palindrome()
     {
        s="null";
     }
 
        palindrome (string s1)
        {
           s=s1;
        }
 
     boolean Ispalindrome()
     {
             string s1 = s.reverse;
             if (s==s1)
             return true;
             else
             return false;
     }
};
 
int main()
{
   palindrome s1("How are you");
   cout<<s1.Ispalindrome();
   palindrome s2("BOB");
   cout<<s2.Ispalindrome();
 
   getch();
   return 0;
}

Thanks for your help in advance!

itchap

Okay, it looks like you're confusing C, C++ and Java.

>#include <string.h>
This supports string handling on array based strings, not the string class. For that you want to include <string>. I know it's confusing, but sadly, that's how it is. :(

>boolean Ispalindrome()
boolean isn't a keyword in C++. You probably want bool instead.

>string s1 = s.reverse;
The string class doesn't have a reverse member, which is fascinating since I'm pretty sure there's a kitchensink member. ;) There's a reverse function in the <algorithm> header, and you pass it a start and end iterator. This might work for you:

#include <algorithm>
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;


class palindrome
{
  string s;

public:

  palindrome()
  {
    s="null";
  }

  palindrome (string s1)
  {
    s=s1;
  }

  bool Ispalindrome()
  {
    string s1 = s;
    
    reverse(s1.begin(), s1.end());

    if (s==s1)
      return true;
    else
      return false;
  }
};

int main()
{
  palindrome s1("How are you");
  cout<<s1.Ispalindrome();
  palindrome s2("BOB");
  cout<<s2.Ispalindrome();

  getch();
  return 0;
}

On a side note, conio.h is a non-standard header, so your code isn't portable. You only include it for getch, and you can replace getch with cin.get to a similar effect without sacrificing portability.

Thank you sooooo much Narue :) I really am getting confused between C, C++ and java. I'm taking all three subjects together....so you must imagine how mixed up im getting ;)

cheers,

itchap

>I'm taking all three subjects together....so you must imagine how mixed up im getting
I know that feeling all too well. I frequent forums for about nine different languages and I end up making more of those blunders than I'd like. ;)

>I'm taking all three subjects together....so you must imagine how mixed up im getting
I know that feeling all too well. I frequent forums for about nine different languages and I end up making more of those blunders than I'd like. ;)

heh....yeah, tell me about it :lol: , goodluck anyway :cheesy:

This article has been dead for over six months. Start a new discussion instead.