0

Good day,

I am quite new to c and trying to find a way to store user integer input into an array as individual digits.

For example,

User input: 2736

I would like it to be stored in array

digits[0] = {2} 
digits[1] = {7} 
digits[2] = {3} 
digits[3] = {6}

I have searched many online discussions but many solutions was to use a for loop and it would not be user friendly if the user is trying to enter a large number for example 209377383838.

Does anyone have a solution to store an input, break it down and store individual number into the array? Thanks for the help!

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
thompsonrapier
  • 155
  • 3
  • 16
  • What did you try so far? You need to show your effort. – 0___________ Oct 23 '21 at 18:55
  • Please note that the English word "number" and "digit" are not the same thing. A digit is a single character from `'0'` to `'9'`, whereas a number can consist of one or several digits. Using incorrect wording is confusing to the reader. I have edited your answer by using the word "digit" where appropriate. – Andreas Wenzel Oct 23 '21 at 19:26
  • **Consider** that your integer, having probably been entered from a keyboard by a human, or transmitted over a service, etc., that it **may already be a string** ... which means you don't need to convert it from integer to string. Indeed, you may need to parse it into an integer later, with all the issues that entails. – UncaAlby Oct 23 '21 at 19:49
  • Do you want the array elements to contain the digits as a character code, i.e. `'0'`, or as an integer data type? For example, do you want the first array element to contain the actual integer `2`, or the character code for `'2'`, which in ASCII is the character code `50`? – Andreas Wenzel Oct 23 '21 at 21:19

2 Answers2

2

You can store the number from user as string and then convert from ASCII format to decimal and store it in your array:

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

int main(){
    char input[1001];
    int arr[1000];
    int i;

    // Take the number as string
    printf("Enter the number: ");
    scanf("%s", input);

    // Convert ASCII to decimal
    for (i = 0; i < strlen(input); i++)
    {
        arr[i] = input[i] - '0';
    }

    return 0;
}
Alaa Mahran
  • 663
  • 4
  • 12
  • input is not an integer. – 0___________ Oct 23 '21 at 19:12
  • Yes, this is the point. So user input for example 12345, the program will store it as a string "12345". Then convert each ASCII characher(for example '1') to decimal. This is the easiest way to achieve that if you don't want the user to enter the number digit by digit. – Alaa Mahran Oct 23 '21 at 19:21
  • there is nothing about the user input. string is not the number. – 0___________ Oct 23 '21 at 19:23
  • I suggest you re-read the user question and test the above code yourself with a simple `printf` for array `arr` at the end – Alaa Mahran Oct 23 '21 at 19:28
  • Thanks! This is what I am looking for! Now I just need to add in validation for the user to enter the specific length of int. Thanks once again!! – thompsonrapier Oct 24 '21 at 02:01
0

You can use sprintf function to put an integer number to a string.

Note1: As the input is a long int, then can't be more than 2,147,483,647.

Note2 : may be interesting to use unsigned long long int, that Contains at least the [0, +18,446,744,073,709,551,615] range. It's specified since the C99 version of the standard.

int main()
{
long int num; 
scanf("%li",&num);

// Convert number to a string
char snum[20];
sprintf(snum,"%ld", num);

printf("Number as string: %s\n", snum);

return 0;
}

[Update]

If the input can be a string type, then you can use the gets function.

#include <stdio.h>
int main()
{
char snum[20];  
gets(snum);
    // this loop is just to show the result.
    for (int i=0;i<strlen(snum);i++){
        printf("digits[%2d] = {%c} \n", i,snum[i]);
    }

return 0;
}

[Input] example: 209377383838

[Output]

digits[ 0] = {2} 
digits[ 1] = {0} 
digits[ 2] = {9} 
digits[ 3] = {3} 
digits[ 4] = {7} 
digits[ 5] = {7} 
digits[ 6] = {3} 
digits[ 7] = {8} 
digits[ 8] = {3} 
digits[ 9] = {8} 
digits[10] = {3} 
digits[11] = {8} 
AziMez
  • 2,014
  • 1
  • 6
  • 16
  • This answer does not address OP's concern of the user entering very high numbers. A `long int` is only guaranteed to be able to represent at least `2,147,483,647`. If the user enters the number mentioned in the question, then the code in this answer has undefined behavior on platforms on which `long int` only has the minimum width required by the ISO C standard. – Andreas Wenzel Oct 23 '21 at 19:31
  • Thank you @AndreasWenzel, I have updated my answer according to your usefull note. – AziMez Oct 23 '21 at 19:48
  • Note that you may also want to consider using a `long long int` (or simply `long long`), which is guaranteed to be at least 64 bits wide. In contrast to `long`, a `long long` is guaranteed to be able to represent the number mentioned in the question. – Andreas Wenzel Oct 23 '21 at 19:51
  • The function [`gets`](https://en.cppreference.com/w/c/io/gets) is no longer part of the ISO C standard. However, you can still use [`fgets`](https://en.cppreference.com/w/c/io/fgets) (I don't recommend using `gets_s` as a replacement, because it is only optionally supported). See this question for the reason why `gets` was removed: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/12149471) – Andreas Wenzel Oct 23 '21 at 19:57
  • @AndreasWenzel, Note2 is added about 'long long int' type. Really interested. – AziMez Oct 23 '21 at 20:00
  • Yes, I like your changes. However, for the stated reasons, your suggestion to use `gets` is bad advice and is preventing me from upvoting your answer. Therefore, I suggest that you change the line `gets(snum);` to `fgets( snum, sizeof snum, stdin );` and also fix the indentation of that line. – Andreas Wenzel Oct 23 '21 at 20:24
  • I'm not sure if it is a good idea to call `strlen` in a loop, because finding the length of a string is an expensive operation. Therefore, you may want to call `strlen` a single time before the loop, and store the returned length in a variable, which you read inside the loop. I guess that some compilers will be able to automatically optimize the call to `strlen` by moving it outside the loop, however I wouldn't rely on all compilers being able to do that. – Andreas Wenzel Oct 23 '21 at 20:27