Iterate over digits by an integer in C

I have an integer, for example, 1191223 and I want to iterate over the numbers. I'm not sure how to do this in C, is there an easy way to do this?

Thank.

+5
source share
8 answers

Forward or backward?

Assuming a positive integer:

  unsigned int n = 1191223;

  while (n != 0) {
       doSomething (n % 10);
       n /= 10;
  }

... will work from the smallest to the largest, or ...

EDIT I ​​forgot everything about this non-working solution that I had here. Please note that Very Smart People ™ seems to consistently use the smallest and largest iteration (both the Linux kernel and GLibC printf, for example, just iterate backwards), but here you can drop back if you really don't want to use snprintfany then the reason ...

int left_to_right (unsigned int n) {
  unsigned int digit = 0;

  if (0 == n) {
    doSomething (0);
  } else {
    digit = pow(10, 1.0+ floor(log10(n)));
    while (digit /= 10) {
      doSomething ( (n / digit) % 10 );
    }
  }
}

, , log10 pow, snprintf,

int left_to_right_fixed_max (unsigned int n) {
  unsigned int digit = 1000000000; /* make this very big */
  unsigned int n10 = 10 * n;

  if (0 == n) {
    doSomething (0);
  } else {
    while (digit > n10) { digit /= 10; }
    while (digit /= 10) {
      doSomething ( (n / digit) % 10 );
    }
  }
}

... , /, .

int left_to_right (unsigned int n) {
  static const unsigned int digit [] = 
    { 1,
      10,
      100,
      1000,
      10000,
      100000,
      1000000,
      10000000,
      100000000,
      1000000000 /* make this very big */
    };
  static const unsigned char max_place = 10;
  /* length of the above array */

  unsigned char decimal;
  unsigned char place;
  unsigned char significant = 0; /* boolean */

  if (0 == n) {
    doSomething (0);
  } else {
    place = max_place;
    while (place--) {
      decimal = 0;
      while (n >= digit[place]) {
        decimal++;
        n -= digit[place];
      }
      if (decimal | significant) {
        doSomething (decimal);
        significant |= decimal;
      }
    }
  }
}

... http://www.piclist.com/techref/language/ccpp/convertbase.htm .

+8

, ( 10). , , 10 s.

, modulo . .

, :

uint32_t n = 1191223;
do {
  uint32_t digit = n%10;
  // do something with digit
}
while (n/=10);

, , :

uint32_t n = 1191223;
#define MAX_DIGITS 10 // log10((double)UINT32_MAX)+1
uint32_t div = pow(10, MAX_DIGITS);

// skip the leading zero digits
while ( div && !(n/div) ) div/=10;
if ( !div ) div = 10; // allow n being zero

do {
  uint32_t digit = (n/div)%10;
  // do something with digit
}
while (div/=10);
+4

, . :

int i = 1191223;
char buffer[16];
char *j;
snprintf(buffer, 16, "%i", i);
for ( j = buffer; *j; ++j ) { /* digit is in *j - '0' */ }
+3

sprintf(), char, , (, ):

int a = 1191223;
char arr[16];
int rc = sprintf(arr, "%d", a);

if (rc < 0) {
    // error
}

for (int i = 0; i < rc; i++) {
    printf("digit %d = %d\n", i, arr[i]);
}
+1
void access_digits(int n)
{
        int digit;
        if (n < 0) n = -n;
        do {
                digit = n % 10;
                /* Here you can do whatever you
                   want to do with the digit */
        } while ((n/=10) > 0);
}
+1

- :

char data[128];
int digits = 1191223;
sprintf(data, "%d", digits);
int length = strlen(data);
for(int i = 0; i < length; i++) {
   // iterate through each character representing a digit
}

Note that if you use an octal number, for example 0100, you also need to change sprintf(data, "%d", digits);to sprintf(data, "%o", digits);.

0
source

The hacky way is to convert this to a string (see strtol), and then reinstall that number. you can use something likecharacter you want - '0'

0
source

Above my head: "i% 100000", "i% 100000", ...

A recursive solution will allow you to start with "i% 10".

0
source

All Articles