Connecting Tech Pros Worldwide Forums | Help | Site Map

Palindrome

Member
 
Join Date: Oct 2006
Location: California, USA
Posts: 45
#1: Dec 6 '06
hi,
im trying to solve the following question:
Create a class responsible for determining whether a string is a palindrome. Show your test cases. Palindome mypal("bob");
mypal.Ispalindrome(); // should return true

i tried to do the code, but im having problems in comlpeting it, I hope that some one can help me know how im supposed to finish it off

this is the code that i did:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. class palindrome
  8. {
  9.       string s;
  10.       palindrome()
  11.       {
  12.          s="null";
  13.       }
  14.  
  15.          palindrome (string s1)
  16.          {
  17.             s=s1;
  18.          }
  19.  
  20.       boolean Ispalindrome()
  21.       {
  22.               string s=s1.reverse;
  23.               if (s2==s1)
  24.               return true;
  25.               else
  26.               return false;
  27.       }
  28. };
  29.  
  30. int main()
  31. {
  32.     palindrome s1("How are you");
  33.     cout<<s1.Ispalindrome();
  34.     palindrome s2("BOB");
  35.     cout<<s2.Ispalindrome();
  36.  
  37.     getch();
  38.     return 0;
  39. }

these are the errors that i get:
1- syntax error before `)' token
2- syntax error before `}' token
3- In function `int main()':
4- ` palindrome:: palindrome(std::basic_string<char, std::char_traits<char>,
5- within this context
note: im using the dev c++ complier

thanks

outofmymind

Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#2: Dec 6 '06

re: Palindrome


fixed a few obvious errors (indicated by // ** comments) in particular your class functions should be public and then is no type boolean - it is bool
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class palindrome
  9. {
  10.       string s;
  11.       public:               // **following members are public
  12.       palindrome()
  13.       {
  14.          s="null";
  15.       }
  16.  
  17.          palindrome (string s1)
  18.          {
  19.             s=s1;
  20.          }
  21.  
  22.       bool Ispalindrome()  // ** change boolean
  23.       {
  24.               string s1=s.reverse();   // ** change operands
  25.               if (s==s1)             // ** test s against s1
  26.               return true;
  27.               else
  28.               return false;
  29.       }
  30. };
  31.  
  32. int main()
  33. {
  34.     palindrome s1("How are you");
  35.     cout<<s1.Ispalindrome();
  36.     palindrome s2("BOB");
  37.     cout<<s2.Ispalindrome();
  38.  
  39.     getch();
  40.     return 0;
  41. }
  42.  
the problem now is that you have no method reverse() to reverse the characters in a string. You need to write one
DeMan's Avatar
Lives Here
 
Join Date: Nov 2006
Location: Adelaide, SA
Posts: 1,748
#3: Dec 6 '06

re: Palindrome


You aould also do this recursively,

1) trim any excess characters (if required, eg if you have to ignore whitespace andpunctuation
2) convert the string to all upper or lower case

3) then (in no particular language):
Expand|Select|Wrap|Line Numbers
  1.  
  2. bool palindrome(String s)
  3. {
  4.   if(s.size==1||s.size==0) //escape case
  5.   {
  6.     return true;
  7.   }
  8.   if(s.first==s.last)
  9.   {
  10.     return palindrome(s.subString(1, s.size-1); //this is for languages where indexingstarts at 0
  11.   }
  12. return false;
  13. }
  14.  
or iteratively you could loop through to halfway comparing beginning and end.
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#4: Dec 6 '06

re: Palindrome


I've tried doing palindromes in many ways, and I think the best way is to use a recursive function as discussed above...that is, as long as you are allowed to even use recursion.
Member
 
Join Date: Oct 2006
Location: California, USA
Posts: 45
#5: Dec 8 '06

re: Palindrome


Quote:

Originally Posted by Ganon11

I've tried doing palindromes in many ways, and I think the best way is to use a recursive function as discussed above...that is, as long as you are allowed to even use recursion.


Thanks for the help everyone,

outofmymind
Reply