Thread: Integers reversed.....

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    6

    Integers reversed.....

    How do I reverse numbers or words to be backwords after I input those numbers or words.

    Like say I input 5789, how do I make it come back out as 9875?

    Any help would be much appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If it is stored as a string, you can use the reverse algorithm. If it is a number, you have to do some math to get each digit and then create a new number with the digits reversed, or you can convert it to a string, reverse it, and convert it back.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    there's a simple way if you are strictly dealing with ints. i know
    that the methods described already are more robust, but just to
    point out that things can be done a little differently, try a loop like
    this:

    Code:
    while (num/10 != 0 || num%10 != 0)
    {  
            printf ("%d", num%10);
            num = num/10;
    }
    this reverses the order of the number, but any leading 0's are
    ignored. the looping condition allows the handling of 0's within
    the number and prevents a loop that either finishes too early or
    runs infinitely. limited functionality, but conveniently simple.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    I got it now, thanks guys.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    i wrote a function to reverse a string, i used it in my base conversion algorithm, use this if you want:

    Code:
    std::string revstr(std::string str) {
      char tmp;
      int l = str.length(), l2 = l - 1, ong;
      for (ong = 0; ong < (l / 2); ong ++) {
        tmp = str[ong];
        str[ong] = str[l2 - ong];
        str[l2 - ong] = tmp;
      }
      return str;
    }
    don't ask about the random variable names / horrible code style.
    it's not too efficient but it's good enough for me.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why would you use that to reverse a string when you can use reverse?
    Code:
    std::reverse(str.begin(), str.end());

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318

    Post

    No need to convert to a string first...
    Code:
    unsigned int reverseDecimalInteger(unsigned int x)
    {
        unsigned int result = 0;
        do {
            result = result * 10 + x % 10;
            x /= 10;
        } while (x > 0);
        return result;
    }
    Last edited by iMalc; 02-05-2006 at 12:56 AM.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    well mine was originally from C, and i didn't know that C++ had a reverse funtion...... i just used it when i ported one of my programs from C -> C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 01:02 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM