splitting numbers into separate digits

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sureshjayaraman
    New Member
    • Dec 2012
    • 1

    splitting numbers into separate digits

    a c++ program to accept a value and split it digit by digit and display in name

    eg:123
    >one two three
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    So 123/100 is 1

    Subtract 100 from 123 to get 23

    23/10 is 2

    Subtract 2* 10 from 23 to get 3.

    That's the algorithm. All you have to now is code it up.

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      another idea would be to read the value into a string, use an array to hold the written representation of each digit, and loop through the input.

      Code:
      string num_text[] = { "zero", "one", "two", ... };
      Code:
      num_text[input.at(loop_index_pos) - '0'];

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        Another way which I think closely mirrors weaknessforcats algorithm is:
        1.)get the number with cin>>
        2.)mod(%) the number with 10 to get the least significant digit as the remainder and store it somewhere.
        3.)divide the number by 10 to lop that digit.
        4.)repeat 2.) and 3.) until digits exhausted
        5.)your store is now full of individual digits.
        6.)fill a container (array or vector) with the ten digit names e.g."zero","one ",..."nine" .
        7.)set up a loop to match each digit with its name and print it.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          This question comes up with some regularity. Take a look at...
          how to print the numbers in words?
          Print a number in words
          Input numbers output words problem
          c program to convert numbers between 1 and 99999
          Converting numbers to spellings (eg, 10 to ten)
          How to convert numerical currency value (33,163.33) into words?
          digits to words

          There are lots more. These are the first few that I found by searching for "number words hundred". The search bar is at the top-right corner of the Bytes page.

          Comment

          Working...