Ammar
Ammar

Reputation: 1233

How to grab some digits from a number?

Let us say we have the following number.

Number = 2001000200030

How can I grab the first digit and store it to a variable? Then grab the next four digits and store them to another variable? Then the next four digits ...

So the output should be like this.

first = 2;

firstfour = 0010

secondfour = 0020

thirdfour = 0030

Thank you and I appreciate your time.

Upvotes: 1

Views: 433

Answers (3)

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>
#include <string.h>
#include <stdint.h>

int main(void){
    int64_t number = 2001000200030LL;
    char data[24];
    char digit[5] ={0}, *p;
    int len, r;
    len=sprintf(data, "%lld", number);
    p = data;
    r = len % 4;
    if(r){
        strncpy(digit, p, r);
        printf("%s\n", digit);
        len -= r;
        p += r;
    }
    while(len!=0){
        strncpy(digit, p, 4);
        printf("%s\n", digit);
        len -= 4;
        p += 4;
    }
    return 0;
}
/* output
2
0010
0020
0030
*/

case of the number was a string, and output to string.

#include <stdio.h>
#include <string.h>

int main(void){
    char number[] = "2005001000200";
    char mode[2]={0};
    char red[5]={0};
    char green[5]={0};
    char blue[5]={0};

    strncpy(mode , &number[0], 1);
    strncpy(red  , &number[1], 4);
    strncpy(green, &number[5], 4);
    strncpy(blue , &number[9], 4);

    printf("Mode = %s\n" , mode);
    printf("Red  = %s\n" , red);
    printf("Green = %s\n", green);
    printf("Blue = %s\n" , blue);
    return 0;
}

Upvotes: 2

Justin Meiners
Justin Meiners

Reputation: 11113

Numbers are stored on the computer in binary, because of this an integer has no capacity to distinguish individual digits.

Convert the number to a string and extract the parts you need. You can also convert each part back into a number if needed. (There are probably prettier ways of extracting parts of the string)

Also this code is for this contrived example, if the integer is unknown, this code cannot safely make the assumptions it does.

long long number = 2001000200030LL;

// convert number to string
char string[64];
sprintf(string, "%lld", number);

// access individual digits
string[0] (first digit)
string[1] (second digit)

// copy first four digits into an int
char firstFour[5]
memcpy(firstFour, string, 4)
firstFour[4] = "\0"; // null terminate
int firstFourInt = atoi(firstFour);

// copy second four digits into an int
char secondFour[5]
memcpy(secondFour, string + 4, 4)
secondFour[4] = "\0"; // null terminate
int secondFourInt = atoi(secondFour);

Upvotes: 2

Rup
Rup

Reputation: 34408

Why use strings when you can stick with processing numbers? You can split by digits using modulo and division in powers of 10.

#include <stdio.h>

int main(void) {
    long long number = 2001000200030LL;

    /* Find power of 10 for top digit */
    long long currentDigit = 1LL;
    while((currentDigit * 10LL) < number) currentDigit *= 10LL;

    printf("First digit = %lld\n", number / currentDigit);
    number %= currentDigit;

    /* Read off groups of four digits */
    while(currentDigit >= 10000LL)
    {
        long long nextFour;
        currentDigit /= 10000LL;
        nextFour = number / currentDigit;
        number %= currentDigit;
        printf("Next four = %04lld\n", nextFour);
    }

    /* Output any remaining digits not covered by a group of four */
    if (currentDigit > 1LL)
    {
        printf("Remaining digits = ");
        for(currentDigit /= 10LL; currentDigit > 1LL; currentDigit /= 10LL)
        {
            printf("%lld", (number / currentDigit) % 10LL);
        }
        printf("%lld\n", number % 10LL);
    }

    return 0;
}

Upvotes: 4

Related Questions