Position of digits

Hi
Someone can help me with a code that i can determine the positions of the digits of an integer.
For example 456
Digit 4 is in position 0
Digit 5 is in position 1
Digit 6 is in position 2
Given an unsigned integer, you can examine the digits one by one in reverse order with code like:
1
2
3
4
5
while (num) {
    unsigned digit = num % 10;
    // Do Something with digit
    num /= 10;
}


With this code, you could find the position from the back. If you also keep track of the length of the number (the number of digits), then you could get the position from the front with a little math.

Another possibility that's much less efficient is to convert the number to a string using ostringstream and then examine the string for the position. One could certainly argue that efficiency doesn't matter since either will run very fast on a modern PC.

Can you clarify some odd cases? Consider the number 456764:
Is digit 4 in position 1? position 6? Both?
If I ask for the position of 8, what should it return?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//======================================================================

vector<int> digitise( unsigned long long N )
{
   vector<int> result;
   while ( N )
   {
      result.push_back( N % 10 );
      N /= 10;
   }
   reverse( result.begin(), result.end() );

   return result;
}

//======================================================================

template <typename T> ostream &operator << ( ostream &strm, const vector<T> &V )
{
   for ( T element : V ) strm << element << " ";
   return strm;
}

//======================================================================

int main()
{
   unsigned long long N;
   cout << "Input a number: ";   cin >> N;

   vector<int> digits = digitise( N );

   for ( int d = 0; d < 10; d++ )
   {
      vector<int> pos;
      for ( int p = 0; p < digits.size(); p++ ) if ( digits[p] == d ) pos.push_back( p );
      if ( pos.size() != 0 ) cout << "Digit " << d << " is at position(s) " << pos << '\n';
   }
}

//====================================================================== 


Input a number: 123454321
Digit 1 is at position(s) 0 8 
Digit 2 is at position(s) 1 7 
Digit 3 is at position(s) 2 6 
Digit 4 is at position(s) 3 5 
Digit 5 is at position(s) 4



Rather more obscure version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

//======================================================================

int length( unsigned long long N ) { return N < 10 ? 1 : 1 + length( N / 10 ); }       // find the digit length of a number

//======================================================================

template <typename T> ostream &operator << ( ostream &strm, const vector<T> &V )        // output a vector of general type
{
   for ( T element : V ) strm << element << " ";
   return strm;
}

//======================================================================

int main()
{
   unsigned long long N;
   cout << "Input a number: ";   cin >> N;
   int len = length( N );

   map<int,vector<int>> pos;                                                // positions for each digit
   for ( int p = len - 1; N; p--, N /= 10 ) pos[N%10].push_back( p );       // update table using digits of N
   for ( auto &e : pos )                                                    // output
   {
      reverse( e.second.begin(), e.second.end() );                          // needed because pushed on "from the back" of N
      cout << "Digit " << e.first << " is at position(s) " << e.second << '\n';
   }
}

//====================================================================== 
Last edited on
this is exceedingly simple if you convert the integer to a string and look at each character. Its much less code, but a tiny bit slower.


Hi Lfs96,

Here's the function :
1
2
3
4
5
6
#include <string>
char getDigitOf(int const number, int const position)
{
	auto numberString = std::to_string(number);
	return numberString.at(position);
}


Invoque it like getDigitOf(456, 1) to get '5'.
If you want a numerical result version, just modify that a bit like this :
1
2
3
4
5
6
#include <string>
int getDigitValueOf(int const number, int const position)
{
	auto numberString = std::to_string(number);
	return numberString.at(position) - '0';
}

And you're good to go :o)

I used the to_string function that converts my integer into a string (of characters).
That's the reason why, when I read an element, I obtain a "char"acter :oP~

Also note that this is the very most simple part... I didn't take care of the "number" before I read the char at that position --> numberString.at(position).

You may want to take care of that and put a little test on "position" to conform the function to what you want and need.
Last edited on
Topic archived. No new replies allowed.