Thread: C++ algorithm help

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    C++ algorithm help

    I am taking a programming class and school and I am trying to write a program. I have tried some stuff but I cant figure out how to do it. What the program does is take an unsigned integer as input calculates if it is a palindrome or not, a number that is the same forwards as it is backwards. I dont want to program written for me I just wont some help on how I would go about this.

    I must write to functions. One that tells me if the integer is a palindrome or not and another that will tell me the digit place in a integer. like if the int was 5589 and I wanted the second place it would return 8.

    Any suggestions about how to go about solving this would be helpful.
    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    21
    try taking the number in as a string. then just flip it and compair it to the original

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If you're looking for speed, try this:
    Code:
    bool checkPalindrome(int testSubject)
    {
    	int tempValue = 0;
    	int origValue = testSubject;
    	int pValue;
    
    	while (testSubject != 0)
    	{
    		pValue = testSubject;
    		pValue %= 10;
    		tempValue *= 10;
    		tempValue += pValue;
    		testSubject /= 10;
    	}
    
    	if(origValue == tempValue)
    	{
    		return true;
    	} else {
    		return false;
    	}
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Any suggestions about how to go about solving this would be helpful.
    There are a number of ways to go about this. Here is my solution this time, the value is reversed into a std::string and tested for a palindrome. The string is reversed because you wanted the indexing function to work from the least signifigant digit to the most signifigant. This way was easier.
    Code:
    #include <iostream>
    #include <string>
    
    class Palin
    {
      int value; // Original value
      std::string buffer; // Reversed value
    
      // Check the value for a palindrome
      bool check_val();
    public:
      Palin ( int val )
        : value ( val )
      {}
    
      // Print the results of check_val()
      void check ( std::ostream& out ); 
      // Get the digit of value located at index
      // in the least signifigant order.
      //   Ex. 1234, get_digit ( 2 ) returns 3.
      int get_digit ( const int index );
    };
    
    bool Palin::check_val()
    {
      int low, high, count = 0;
      int copy = value;
    
      // Copy the integer to the buffer in reverse
      for ( count = 0; copy != 0; count++ ) {
        buffer += ( copy % 10 ) + '0';
        copy /= 10;
      }
    
      // Check for a palindrome
      for ( low = 0, high = count - 1; low < high; low++, high-- ) {
        if ( buffer[low] != buffer[high] )
          return false;
      }
    
      return true;
    }
    
    void Palin::check ( std::ostream& out)
    {
      if ( check_val() )
        out<< value <<" is a palindrome.\n";
      else
        out<< value <<" is a not palindrome.\n";
    }
    
    int Palin::get_digit ( const int index )
    {
      return buffer[index - 1] - '0';
    }
    
    int main()
    {
      Palin my_pal ( 1221 );
    
      my_pal.check ( std::cout );
      std::cout<<"The second digit is "<< my_pal.get_digit ( 2 ) <<'\n';
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    Howdo I flip the string around?

    how do I flip a string around?

    Thanks

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how do I flip a string around?
    You can either copy it to another buffer in reverse, or swap elements from the outside in until you get to the middle:
    Code:
    copy to a buffer:
    
    step 1: 1234 5
    step 2: 123  54
    step 3: 12   543
    step 4: 1    5432
    step 5:      54321
    
    swap elements:
    
    step 1: 12345
    step 2: 52341
    step 3: 54321
    -Prelude
    My best code is written with the delete key.

  7. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    how do I flip a string around?


    You could iterate backwards

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
       string name;
       string reverse;
       
       cout << "Enter a name: ";
       cin >> name;
       
       for (int i = name.size() -1; i > -1; i--)
          reverse += name[i];
          
       cout << "Reverse name: " << reverse << endl;
       return 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if it's not for school you could use strrev();

    char forward[] = "hello";
    char reverse[20];
    strcpy(reverse, strrev(forward));

    Teacher might frown on such a short cut however.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if it's not for school you could use strrev();
    And if your compiler supports it, strrev is not ISO standard.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM