-1

I have an integer, 20230302. I need to extract and store the 1st 4 digits (that is, 2023) in one integer variable, 03 in another integer variable and finally 02 in another integer variable. How do I do this?

My attempt:

#include <stdio.h>

int main()
{
    int num = 0;
    int power = 1;

    printf("Enter any number:");
    scanf("%d", &num);

    while (num > power)
        power *= 10;

    power /= 10;

    while (num != 0) {
        int digit = num / power;
        printf("%d\n", digit);
        if (digit != 0)
            num = num-digit * power;
        if (power != 1)
            power /= 10;
    }

    return 0;
}

However, this code extracts all digits individually.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 3
    1. Open your favorite text editor or IDE. 2. Try to write some code to do what you want by youreself. 3. Ask *specific questions* with a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) if you have any. – MikeCAT Mar 02 '23 at 13:33
  • @MikeCAT: MREs are for debugging questions, not all questions. – Eric Postpischil Mar 02 '23 at 13:34
  • Hint, In the code you will use to attempt this, use string parsing. – ryyker Mar 02 '23 at 13:34
  • try searching for "split string" and then "convert string to int" - try it out, and if you still have issues, post a question here, but read [ask] first – JoSSte Mar 02 '23 at 13:35
  • @ryyker I disagree because the input is an integer. – MikeCAT Mar 02 '23 at 13:35
  • Integers aren't really good for holding full dates in a format like that. I recommend strings instead. With that said, normal decimal arithmetic is the key, like the one you learned in basic school. – Some programmer dude Mar 02 '23 at 13:35
  • @MikeCAT - It does not have to be. `printf("Enter any number:"); if (fgets(line, 100, stdin)) {//parse here}` There is no reasonable logic that would be as simple as just reading value in as a string, then parsing into three parts. – ryyker Mar 02 '23 at 13:38
  • Here is an demonstration in C that [splits a string](https://www.geeksforgeeks.org/split-given-string-into-substrings-of-size-k-by-filling-elements/) into 3 segments. You can easily adapt the code here to your problem. Once you have the 3 strings segmented properly, you can either keep them as strings, or convert them back into integers using `atoi()` or `strtol()` – ryyker Mar 02 '23 at 13:48
  • @MikeCAT - OP has done what you ask, consider a reopen vote. – ryyker Mar 02 '23 at 13:50
  • 3
    Hint: What is `20230302 / 100`? Hint: What is `202303 % 100`? – Some programmer dude Mar 02 '23 at 13:55
  • 1
    sprintf the int into a buffer, then strncpy parts into another buffer and atoi it to an int variable would be my approach – Thomas Bobek Mar 02 '23 at 14:35
  • 1
    @ThomasBobek Both `strncpy` and `atoi` are among the standard lib functions that should always be avoided. Use `strcpy` and `strtol` instead. – Lundin Mar 02 '23 at 14:40
  • @Lundin I think `strncpy` is safer to use compared to `strcpy` as it limits length and thus cannot exceed bounds if properly used. – Thomas Bobek Mar 03 '23 at 05:55
  • 1
    @ThomasBobek The `strncpy` is safer because used correctly (I've seen plenty of incorrect uses here on SO) it won't lead to buffer overflows. On the other hand it's *unsafe* because there's a case where the string null-terminator will not be added, leading to a lot of confusion and debugging to find out. YMMW etc. :) – Some programmer dude Mar 03 '23 at 06:48
  • 1
    @ThomasBobek That's a myth and it's not correct. See [Is strcpy dangerous and what should be used instead?](https://software.codidact.com/posts/281518) for details. – Lundin Mar 03 '23 at 07:24
  • check out these, see if they can help: https://stackoverflow.com/questions/4261589/how-do-i-split-an-int-into-its-digits https://stackoverflow.com/questions/4615046/c-get-each-digit-in-int https://stackoverflow.com/questions/52380912/separating-digits-in-int-into-separate-ints?noredirect=1&lq=1 https://stackoverflow.com/questions/49849268/how-to-get-just-specified-numbers-from-a-large-number?noredirect=1&lq=1 – Abderrahmene Rayene Mihoub Mar 04 '23 at 14:35

1 Answers1

0

You can extract numbers expressed in ranges of digits directly using division and remainder operations:

#include <stdio.h>

int main() {
    int num;

    printf("Enter any number: ");
    if (scanf("%d", &num) != 1)
        return 1;

    // dividing by 10000 removes the last 4 digits.
    // modulo 10000 extracts the last four digits.
    // division and modulo have the same precedence and group left to right
    // hence (num / 10000) % 10000 can be written without parentheses
    int year  = num / 10000 % 10000;
    int month = num / 100 % 100;
    int day   = num % 100;

    printf("%02d/%02d/%04d\n", day, month, year);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189