Reversing a number

Discussion in 'C++' started by deanfamily11, Sep 11, 2005.

  1. deanfamily11

    deanfamily11 Guest

    I need to write a function that takes an integer as a parameter and then
    returns it reversed (i.e. input: 45678, returned: 87654). Can anyone help?
     
    deanfamily11, Sep 11, 2005
    #1
    1. Advertising

  2. * deanfamily11:
    > I need to write a function that takes an integer as a parameter and then
    > returns it reversed (i.e. input: 45678, returned: 87654).


    Of course, that problem pops up in all kinds of everyday situations.


    > Can anyone help?


    Start by writing out the digits in right-to-left order, by using integer
    division in a loop. Example: 45678/10 = 4567 when "/" denotes integer
    division. And what do you get when you multiply the result by 10?


    Follow-up set to [comp.programming].
    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Sep 11, 2005
    #2
    1. Advertising

  3. you can convert the inetger to a string, now a string is a just a vector,
    you can reverse it easily, then you can convert back to integer.


    "deanfamily11" <> wrote in message
    news:N1OUe.879$R9.111@trnddc02...
    >I need to write a function that takes an integer as a parameter and then
    >returns it reversed (i.e. input: 45678, returned: 87654). Can anyone help?
    >
     
    Someonekicked, Sep 11, 2005
    #3
  4. deanfamily11

    Mike Wahler Guest

    "deanfamily11" <> wrote in message
    news:N1OUe.879$R9.111@trnddc02...
    >I need to write a function that takes an integer as a parameter and then
    >returns it reversed (i.e. input: 45678, returned: 87654). Can anyone help?


    #include <iostream>
    #include <sstream>
    #include <string>

    int rev(int value)
    {
    std::stringstream ss;
    ss << value;
    std::string s(ss.str());
    std::reverse(s.begin(), s.end());
    ss.str(s);
    int result(0);
    ss >> result;
    return result;
    }

    int main()
    {
    std::cout << rev(45678) << '\n';
    return 0;
    }

    -Mike
     
    Mike Wahler, Sep 11, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The Devil's Advocate©

    Reversing a marquee tag

    The Devil's Advocate©, Oct 11, 2004, in forum: HTML
    Replies:
    22
    Views:
    2,402
    Sentient Fluid
    Oct 23, 2004
  2. Kevin

    Help: 2D Array Reversing

    Kevin, Oct 3, 2003, in forum: C++
    Replies:
    4
    Views:
    1,048
    Thomas Matthews
    Oct 6, 2003
  3. Dr. David Kirkby

    Reversing the order of some loops.

    Dr. David Kirkby, Oct 22, 2003, in forum: C Programming
    Replies:
    4
    Views:
    337
    Glen Herrmannsfeldt
    Oct 26, 2003
  4. netforce

    reversing a string - newbie question

    netforce, Mar 2, 2004, in forum: C Programming
    Replies:
    9
    Views:
    544
    Old Wolf
    Mar 8, 2004
  5. flipflop

    Reversing order of quicksort

    flipflop, May 28, 2004, in forum: C Programming
    Replies:
    3
    Views:
    366
    Martin Ambuhl
    May 28, 2004
Loading...

Share This Page