C: how to break apart a multi digit number into se

2020-01-29 07:15发布

Say I have a multi-digit integer in C. I want to break it up into single-digit integers.

123 would turn into 1, 2, and 3.

How can I do this, especially if I don't know how many digits the integer has?

11条回答
看我几分像从前
2楼-- · 2020-01-29 07:51

I made this based on the code from @asaelr:

typedef struct digitsArrayPlusNumber {
    uint32_t *baseAddress;
    uint32_t number;
} digitsArrayPlusNumber;

digitsArrayPlusNumber *splitDigits (uint32_t inValue) {
    // based on code from asaelr@stackoverflow.com

    uint32_t inputValue = inValue;

    //Count digits

    uint32_t theCount = 1;
    while (inputValue /= 10)
        theCount++;

    // put in array
    uint32_t *arr = malloc(sizeof(uint32_t) * theCount);
    uint32_t dig = theCount;
    while (dig--) {
        arr[dig]=inValue % 10;
        inValue /= 10;
        //  printf ("%d\n", arr[dig]);
    }

    digitsArrayPlusNumber *dandn = malloc (sizeof(digitsArrayPlusNumber));

    dandn->baseAddress = arr;
    dandn->number = theCount;

    return dandn;

}

int main(int argc, const char * argv[]) {


    for (int d = 0; d < splitDigits(12345678)->number; d++)
        printf ("%u\n", (splitDigits(12345678)->baseAddress)[d]);

}

It works quite well, thanks!

查看更多
迷人小祖宗
3楼-- · 2020-01-29 07:51

You can divide and conquer but you have rewrite all of arithmetic libraries. I suggest using a multi-precision library https://gmplib.org But of course it is good practice

查看更多
forever°为你锁心
4楼-- · 2020-01-29 07:53
int value = 123;
while (value > 0) {
 int digit = value % 10;
 // do something with digit
 value /= 10;
}
查看更多
贼婆χ
5楼-- · 2020-01-29 07:54

we can use this program as a function with 3 arguments.Here in "while(a++<2)", 2 is the number of digits you need(can give as one argument)replace 2 with no of digits you need. Here we can use "z/=pow(10,6)" if we don't need last certain digits ,replace 6 by the no of digits you don't need(can give as another argument),and the third argument is the number you need to break.

int main(){
long signed c=0,z,a=0,b=1,d=1;
scanf("%ld",&z);
while(a++<2){
       if(d++==1) 
       z/=pow(10,6);
       c+=(z%10)*b; 
       z/=10;
       b*=10;}
        return c;}
查看更多
Fickle 薄情
6楼-- · 2020-01-29 07:56
int l1; //123456 for example
scanf("%d",&l1);
char s[sizeof(l1)];
sprintf(s,"%5d",l1);'

//This will give you separate digits of the number in char format inside s[0],s[1] 
//and so on.

//If you want them in int format, declare a int array say int i[sizeof(l1)] and add 
//the following code

for(int c=1;c<=sizeof(l1);c++){
i[c] = s[c] - '0';
} 

//Now i[0], i[1] etc will have the digits in int format
查看更多
贪生不怕死
7楼-- · 2020-01-29 07:57

The last digits of 123 is 123 % 10. You can drop the last digit of 123 by doing 123/10 -- using integer division this will give you 12. To answer your question about "how do I know how many digits you have" -- try doing it as described above and you will see how to know when to stop.

查看更多
登录 后发表回答