What is the easiest way to find the position of a digit inside an integer or long number in C language like 4325262464908 for instance?
-
1Please give an example with the expected output. – John Zwinck Oct 24 '20 at 07:53
-
1I have a long integer like 5212467824952 .. how can I get the position of the digit 1 for instance @JohnZwinck – Muhammad Abd-Elsattar Oct 24 '20 at 07:56
-
3easiest is to start with a string; if the number comes from the user that's the primordial data type anyway – pmg Oct 24 '20 at 07:57
-
I can't because I will perform math operations on it later on my program @pmg – Muhammad Abd-Elsattar Oct 24 '20 at 07:59
-
You can have both @MuhammadAbd-Elsattar... `fgets()` to get a string **and** `strtol()` to convert to integer (and use different objects to hold them) – pmg Oct 24 '20 at 08:00
-
BTW: your example `4325262464908` needs 42 bits (+1 for sign). Your `long`s may be too small for it. – pmg Oct 24 '20 at 08:10
-
2Where is your code? What is the definition of the easiest? – 0___________ Oct 24 '20 at 08:11
-
here is my code `#include
#include – Muhammad Abd-Elsattar Oct 24 '20 at 08:17#include #include #include int main(void) { long number = get_long("Number\n"); int length = floor(log10(labs(number))) + 1; printf("length: %i\n",length); }` -
1How do you count the position? Is the 1 the third digit from the left, or 11th digit from the right? Also, if search for 5, which of the two occurrences should be reported? What about searching for 2? – Jonathan Leffler Oct 24 '20 at 08:17
-
You should edit your code into your question, Muhammad. And it isn't clear what relation the code you just put in the comment has to what you ask in the question. Also, supplying zero presents problems to `log10()`, unless my memory of high school maths has gone completely haywire. – Jonathan Leffler Oct 24 '20 at 08:18
-
the easiest like the less complicated way .. like if i want to get the digit in position 2 in a string like `str = "4457834"` i can do `str[2]` .. what is the equivalent way to do so if its an int @P__J__ – Muhammad Abd-Elsattar Oct 24 '20 at 08:20
-
`digit in position 2 i can do str[2]` - do you count digits `0`? With `char*str="4457834"` then `str[2]` is `5`. `what is the equivalent way to do so if its an int` Convert `int` to a string. Do the same. – KamilCuk Oct 24 '20 at 08:20
-
my code in the comment explains how to find the length of the integer the user inputs .. i haven't worked on the part where I can find the position of a digit in this integer yet @JonathanLeffler – Muhammad Abd-Elsattar Oct 24 '20 at 08:24
2 Answers
You can use itoa() function to convert your integer value to a string.
See this question on how to.
But long story short, the declaration of itoa() is:
char * itoa ( int value, char * str, int base );
Where:
- value is the number you want to convert to string.
- str the pointer to a char array where to store the resulting string.
- base the numeric base to use for the conversion (ex. 10 for decimal).
Once you have your array you can use a loop the array character by character to find your number and print the position.
char c = str[0];
int n = 0;
while (c != 0)
if (c == '0' + my_number)
break;
At the end of the loop n will contain the position of the first occurrence of your number. I would suggest to check if c==0 to be sure the string was not empty or that you didn't reach the end of the string.

- 730,956
- 141
- 904
- 1,278

- 54
- 4
-
1There's also `snprintf()`, which has the merit of being more portable. And `strchr()` can probably be used — though that's not quite such a given. – Jonathan Leffler Oct 24 '20 at 08:13
-
Thanks for the edit. And yes, indeed, itoa is not part of c/c++ (it is widely supported though); but it does have the ease of use and the variable numeric base in its favor. – Juan Oct 24 '20 at 08:21
-
i watched a youtube tut on how to use this function and here's how I implement it .. i first declared my integer and targeted string ` int number = get_int("Number\n"); ` ` char mystr[10]; ` then the function ` itoa(number, mystr, 10); ` but I got a problem ` implicit declaration of function 'itoa' is invalid in C99 ` – Muhammad Abd-Elsattar Oct 24 '20 at 11:07
-
That's probably because itoa() is not supported by your compiler. Try using what Jonathan proposes instead: `snprintf()` [link](http://www.cplusplus.com/reference/cstdio/snprintf/) – Juan Oct 24 '20 at 11:48
Here is a solution that does not involve strings, which means will be much faster than the string-based approaches in most cases.
Divide by 10 until the rightmost digit is the one you're looking for, then keep dividing by 10 until you reach zero:
int find_digit(long unsigned num, unsigned digit) {
int pos = -100;
while (num) {
unsigned right = num % 10;
num /= 10;
if (right == digit)
pos = 0;
else
++pos;
}
return pos;
}
It returns leftmost index where the digit was found. If the digit was not found, it returns a negative number (specifically the length of the number in base 10, minus 100).

- 239,568
- 38
- 324
- 436
-
-
@P__J__: My solution does return the index from the left, of the first occurrence nearest the left. – John Zwinck Oct 24 '20 at 09:08
-