2

I'm new to programming and stuck in a problem. I have a long variable of any length. I want to extract any number of digits of this number. An Example: Number = 135678256 and I want the first two digits, here 13. I added some pseudo-code, which shall represent my first idea.

int digits(long n){
int x = n / (10^ length(long n)) ;
int y = n / (10^(length(long n)-1)) ;
y = x-y;
return x,y};

length is a function that returns the length of Number.

How to change my code efficiently to get the fourth and fifth digits? Thanks a lot!

phil111
  • 23
  • 3
  • What have you tried so far, and where are you stuck? – Arya McCarthy Sep 01 '17 at 14:14
  • 1
    What do you mean with "long variable of any length"? The type `long` will have a fixed size (usually either 4 or 8 bytes depending on platform and compiler). Or do you mean any number of *digits* (within the limits of a signed 32 or 64 bit integer)? – Some programmer dude Sep 01 '17 at 14:15
  • 2
    Also, I assume that the code you show is *pseudo-code*? Not something you really attempted to use in a real program? – Some programmer dude Sep 01 '17 at 14:15
  • 1
    I just tried to get it done with pseudo-code to solve the problem of getting the first two digits and now I am stuck by getting any two consecutive digits. Yes, I mean any number of digits. First, I want to solve the logical problem and then write the actual code. – phil111 Sep 01 '17 at 14:22
  • 1
    Then I suggest you edit your question to explicitly state that the code is pseudo-code, or you would get many unrelated comments and close-votes. – Some programmer dude Sep 01 '17 at 14:23
  • 1
    Possible duplicate of [Finding a Specific Digit of a Number](https://stackoverflow.com/questions/4410629/finding-a-specific-digit-of-a-number) – Rivasa Sep 01 '17 at 14:27
  • 2
    Use modulus, `%`, to cut off leading digits. `n%1000` would return the last three digits of `n`. – Klas Lindbäck Sep 01 '17 at 14:32
  • 1
    What you are trying to do -- extract any sequence of digits (as ASCII) from a binary integer -- amounts to writing a version of the standard library function `printf`, but less generally: just for long ints. You have probably seen `printf`; it converts binary numbers to sequences of ASCII characters -- the one we humans read and write (as a side effect they are written somewhere later, but the main effort is the conversion). Because you need any sequence of characters from the string representation of the number you can just write the general function and then substring the result as needed. – Peter - Reinstate Monica Sep 01 '17 at 14:32
  • `long n` is a _signed integer_. What should `int digits(-123)` return ? – chux - Reinstate Monica Sep 01 '17 at 14:41
  • Actually, you are rather writing an `ltoa()` function. Kernighan and Ritchie covered that in *The C Programming Language*; it is quoted in [this](https://stackoverflow.com/a/29544416/3150802) reply on StackOverflow. I'm sure it will be inspirational. – Peter - Reinstate Monica Sep 01 '17 at 14:41
  • "I want the first two digits," , what should `int digits(6)` return? – chux - Reinstate Monica Sep 01 '17 at 14:42
  • 1
    Note that suggested duplicate is to a C++ post. Candidate answers in C differ, even if some overlap. – chux - Reinstate Monica Sep 01 '17 at 14:44
  • Your pseudo code seems to end up x==0, y==1, x-y==-1. Doesn't it? If you use length-1 and length-2 you end up with x==1, y==13, x-y==-12. – Yunnosch Sep 01 '17 at 22:31
  • Finally, I used an easier equation in pseudo-code number / 10^(length(number)-x). x-represents the number of digits I want. It only solves the problem for digits in the beginning but that is okay for now. – phil111 Sep 02 '17 at 14:24

0 Answers0