Palindromes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elsa
    New Member
    • Jan 2007
    • 29

    Palindromes

    hi everybody..i need to define two functions that test for word Palindromes..

    1- void bool isPalindromeUsi ngIteration(str ing ) that takes a string as a paremeter and tests if it is a Palindrome using a loop structure.

    2- bool isPalindromeUsi ng Recursion(strin g) that takes a string as paremeter and tests if it is a Palindrome using a recursion.

    i need to explore the standard c++ library string and cctype to be able to define these two functions..
    i want to use arrays in the progarm.. can u please help me..
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    What ideas do you have? What code have you got so far?

    A good start would be to figure out how you'll test for a palindrome. To understand this, you'll need to know what a palindrome is - understanding its nature will help your test.

    Comment

    • elsa
      New Member
      • Jan 2007
      • 29

      #3
      i know what palindromes are..and i know i that i have to use the for loop and try to find out if the firt letter is the same as the last letter..but how can i start and how can i use the arrays to do that? plz help me

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Well, if you are using strings, you can treat them as an array of characters. Thus, you can use the brackets [] to get a character at a certain position and compare it to the last character, etc.

        How will you check using recursion?

        Comment

        • elsa
          New Member
          • Jan 2007
          • 29

          #5
          i dont really know what recursion means..this thing is a part of an assignment that is due on the 21st of january..the first part of the assignment is to build a program that will convert a decimal number into a binary number using arthmetic operators and bitwise operators..i did the first part as this:
          #include<iostre am>
          #include<string >
          using std::string;
          using std::cout;
          using std::cin;
          using std::endl;

          // Here are some function declarations

          unsigned short getMenuSelectio n( );
          void binaryConversio nUsingArithmeti cOperators(unsi gned long);
          void binaryConversio nUsingBitwiseOp erators (unsigned long);

          // Utilityfunction s
          void initializeArray (unsigned short[],int) ;

          void printBinary(uns igned short[ ],int) ;


          int main()
          {
          unsigned short choice;
          do
          {
          choice = getMenuSelectio n();

          cout <<"You chose Menu Option #"<<choice<<end l ;
          switch(choice)
          {
          case 1 :
          unsigned long a;
          cout<<" Please enter an integer " ;
          cin>>a ;
          binaryConversio nUsingArithmeti cOperators(a);
          break ;
          case 2 :
          cout<<" Please enter an integer " ;
          cin >>a ;
          binaryConversio nUsingBitwiseOp erators(a) ;
          break ;
          case 3 :
          cout<<"Please enter a string ";
          break;
          case 4:
          cout<<"Please enter a string ";
          break;
          case 5:
          break;
          default:
          cout<<"you should never get here\a\a\a\a!!! ";
          }
          }while(choice != 5);
          cout<<"you choose to quit\a\a\a"<<en dl;
          return 0;
          }
          unsigned short getMenuSelectio n()
          {
          unsigned short selection=0;
          do
          {
          cout<<"-1- Binary Conversion using Arithmetic Operators"<<end l;
          cout<<"-2- Binary Conversion using Bitwise Operators"<<end l;
          cout<<"-3- Testing for Palindroms Using Iteration"<<end l;
          cout<<"-4- Testing for Palindroms Using Recursion"<<end l;
          cout<<"-5- Quit"<<endl;
          cout<<"please enter your selection <1-5>:"<<endl;
          cin>>selection;
          }
          while(selection <1 || selection>5);
          return selection;
          }
          void binaryConversio nUsingArthemati cOperators(unsi gned long x)
          {
          unsigned short binaryDigit[100], numberOfDigits= 0;
          initializeArray (binaryDigit,10 0);
          do
          {
          binaryDigit[numberOfDigits+ +]=x%2;
          x=x/2;
          }while(x!=0);
          printBinary(bin aryDigit,number OfDigits);
          }

          void binaryConversio nUsingBitwiseOp erator(unsigned long x)
          {
          unsigned short binaryDigit [100],
          numberOfDigits= 0;
          initializeArray (binaryDigit , 100);
          do
          {
          binaryDigit[numberOfDigits+ +]=x&1;
          x=x>>1;
          }while(x!=0);
          printBinary(bin aryDigit, numberOfDigits) ;
          }

          void initializeArray (unsigned short a[], int size)
          {
          for(int i=0;i<size; i++)
          a[i]=0;
          }

          void printBinary(uns igned short a[], int size)
          {
          for(int i=size-1; i>=0; i--)
          cout<<a[i];
          cout<<endl;
          }
          but the second part was hard..and i left things unwritten..and i really need the help..

          Comment

          Working...