C: how to break apart a multi digit number into separate variables?
Asked Answered
A

11

32

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?

Altruistic answered 15/2, 2012 at 22:46 Comment(1)
If your int started life as char data (input from user or text file ...) do not convert to int in the first place (no scanf, no atoi ...) and use the characters to separate the digits.Rodroda
A
55
int value = 123;
while (value > 0) {
 int digit = value % 10;
 // do something with digit
 value /= 10;
}
Albina answered 15/2, 2012 at 22:52 Comment(0)
P
9

First, count the digits:

unsigned int count(unsigned int i) {
 unsigned int ret=1;
 while (i/=10) ret++;
 return ret;
}

Then, you can store them in an array:

unsigned int num=123; //for example
unsigned int dig=count(num);
char arr[dig];
while (dig--) {
 arr[dig]=num%10;
 num/=10;
}
Pion answered 15/2, 2012 at 22:55 Comment(0)
S
2

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.

Sphenic answered 15/2, 2012 at 22:53 Comment(0)
S
1

As a hint, getting the nth digit in the number is pretty easy; divide by 10 n times, then mod 10, or in C:

int nthdig(int n, int k){
     while(n--)
         k/=10;
     return k%10;
}
Scherzo answered 15/2, 2012 at 22:51 Comment(3)
this is not nth digits, this is nth digits starting from the least significantSphenic
You don't consider the least significant digit to be the zeroth?Scherzo
well, I consider first or zeroth digits of 123 to be 1, not 3.Sphenic
A
0

I think below piece of code will help....

temp = num;
while(temp)
{
    temp=temp/10;
    factor = factor*10;
}

printf("\n%d\n", factor);
printf("Each digits of given number are:\n");

while(factor>1)
{
    factor = factor/10;
    printf("%d\t",num/factor);
    i++;
    num = num % factor;
}
Attainment answered 1/11, 2013 at 16:31 Comment(0)
P
0
//Based on Tony's answer
#include <stdio.h> 
int nthdig(int n, int k){
    while(n--)
        k/=10;
    return k%10;
}

int main() {
    int numberToSplit = 987;
    printf("Hundreds = %i\n",nthdig(2, numberToSplit));
    printf("Tens     = %i\n",nthdig(1, numberToSplit));
    printf("Units    = %i\n",nthdig(0, numberToSplit));
}

This results in the following printout:

Hundreds = 9

Tens = 8

Units = 7

Pharyngology answered 17/4, 2017 at 21:50 Comment(0)
D
0

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 [email protected]

    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!

Deviate answered 17/6, 2017 at 19:48 Comment(0)
P
0

You can use %10, which means the remainder if the number after you divided it. So 123 % 10 is 3, because the remainder is 3, substract the 3 from 123, then it is 120, then divide 120 with 10 which is 12. And do the same process.

Pak answered 21/12, 2017 at 21:22 Comment(0)
B
0
        //This is c#, but you can see how easy it would be to convert
        //to another language.

        int myNumber = 123456;
        string myWord = Convert.ToString(myNumber);

        string partOne = myWord.Substring(0, 2);
        string partTwo = myWord.Substring(2, 2);
        string partThree = myWord.Substring(4, 2);

        int num01 = Convert.ToInt32(partOne);
        int num02 = Convert.ToInt32(partTwo);
        int num03 = Convert.ToInt32(partThree);

        Console.WriteLine(num01);
        Console.WriteLine(num02);
        Console.WriteLine(num03);

        Output:
        12
        34
        56
Bridging answered 5/3, 2023 at 19:39 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Marital
V
-2

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;}
Vadim answered 13/9, 2015 at 13:53 Comment(1)
Never use pow for thisMetropolis
A
-3

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

Alexi answered 15/9, 2017 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.