planetoidLV
planetoidLV

Reputation: 49

How can you convert a long into an array in c?

I'm working on a project that does an algorithm with the given input. I have the input stored in a long. with the given input, I need to convert “number” into an array so I can have access to each digit. for example, if “number = 73757383” I need to convert that into an array: array = [7, 3, 7, 5, 7...] to be able to do the algorithm (multiply every other digit, then add the others together). But I can't seem to figure out how to do this. Any help is appropriate. Also just as a note, I'm using the cs50 library. Thank you!

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(){

    long credit;
    int number;
    int arr[digit];
    int i;

    do
    {
        credit = get_long("Number: ");
        number = floor(log10(credit) + 1); 

    }
    while (number < 13 || number > 16); 

Upvotes: 0

Views: 1123

Answers (4)

chqrlie
chqrlie

Reputation: 144810

You could simply compare the number with 1000000000000 (at least 13 digits) and 9999999999999999 (at most 16 digits), but you probably need the number of digits later in the code:

#include <stdio.h>
#include <cs50.h>

int main() {
    long credit;
    int arr[16];
    int i;

    do {
        credit = get_long("Number: ");
    }
    while (credit < 1000000000000 || credit > 9999999999999999);

    ...

Alternative:

#include <stdio.h>
#include <cs50.h>

int main() {
    long credit;
    int arr[16];
    int number, i;

    do {
        credit = get_long("Number: ");
        number = snprintf(NULL, 0, "%lu", credit);
    }
    while (number < 13 || number > 16);

    for (i = number; i --> 0;) {
        arr[i] = credit % 10;
        credit /= 10;
    }

Also note that type long may be too small to accommodate numbers larger than 231. You should use long long or even unsigned long long for this or better use a string.

Upvotes: 1

Chanhee Cho
Chanhee Cho

Reputation: 51

  1. using % operator can be a good solution
while(input != 0) {
  digit = input%10;   // store this one
  input = input/10;
}
  
  1. alternatively, you can use 'sprintf' for converting the input to a string. Then, you can access every digit using the index of each digit.

Upvotes: 0

Enrico Migliore
Enrico Migliore

Reputation: 229

You may use a long to ascii function:

ltoa()

or the the well known and standard library function:

sprintf()

In the ascii buffer you can access each individual digit as a char digit.

If you need an array of integer instead, just subtract '0' to each char digit:

buffer[k] - '0'

Upvotes: 0

mch
mch

Reputation: 9804

The easiest way is to create a string out of the number and then process 1 digit after another:

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

int main(){

    long credit = 4564894564846;
    int arr[20];
    char buffer[21];

    sprintf(buffer, "%ld", credit);
    int n = strlen(buffer);
    for (int i = 0; i < n; i++)
        arr[i] = buffer[i] - '0';
    
    for (int i = 0; i < n; i++)
        printf("%d\n", arr[i]);
}

https://godbolt.org/z/YY4znd

Another possibility is to extract the digit from the number with % 10 and divide by 10 afterwards, but then you get the digits in the wrong order.

Upvotes: 1

Related Questions