3

I am attempting to learn C by myself. I was trying to split an integer into multiple separate integers, e.g. 12345 into 12 and 34 and 5, but I'm unable to find a way to do it.

I already know some Java and programming basics. Would I need to use some kind of do-while or for loop to do this, or can I use an array of an integer?

Anders
  • 8,307
  • 9
  • 56
  • 88
Mukul Ram
  • 356
  • 4
  • 14
  • `sprintf()` it? C doesn't really have the sort of facility you seem to want, it has no particular functions for decimal numbers. – EOF Oct 04 '15 at 20:11
  • I think you mean "split", not "splice" - they are opposite operations. – clearlight Oct 04 '15 at 20:16
  • What criteria do you want to split the number on? Is it mathematically based, positionally based, or is there some other way you're deciding to split up the number? Are you looking for a generic solution to split up any given number or just 12345? Is there a preference as to whether you handle it arithmetically or through string parsing operations? – clearlight Oct 04 '15 at 20:18
  • So you have an `int` and want several `int`s? How about some simple mathematical operations then? (integer division and modulo)? If that's not the scenario, please clarify and ... show code. –  Oct 04 '15 at 20:20
  • @nerdistcolony Yes, you're right. I didn't know that until I looked it up. – Mukul Ram Oct 04 '15 at 20:29
  • @nerdistcolony Positionally based - it needs to be a separation of 2-2-4. And always with an 8 digit number. So an input of 12345678 would need to be separated into 12, 34 and 5678. – Mukul Ram Oct 04 '15 at 20:31
  • @FelixPalmen You mean do a % to get the remainder everywhere? That's actually a good idea. I didn't think of that. Was focusing more on syntax. – Mukul Ram Oct 04 '15 at 20:34
  • @MukulRam ok I'll update my answer for 12345678 – clearlight Oct 04 '15 at 20:36

3 Answers3

2

This dynamically accomplishes what you are looking for! You can set any type of split you want. 2-2-4 or 3-4-5 or anything. (you can basically get a number string from the user and accomplish this task, and turn the temporary string into an integer if you would like later on) :

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

int main() {
    int i; //Counter

    char getStr[100];
    int NumberToSplit1, NumberToSplit2, NumberToSplit3;

    printf("Enter a number: ");
    scanf("%s", getStr);    //get it as a string
    //or you can use scanf("%[^\n], getStr);

    printf("How many number splits you want: ");
    scanf("%d %d %d", &NumberToSplit1, &NumberToSplit2, &NumberToSplit3);

    printf("\n%d-%d-%d split: \n", NumberToSplit1, NumberToSplit2, NumberToSplit3);

    for (i = 0; i < NumberToSplit1; i++) {
        printf("%c", getStr[i]);
    }
    printf("\n");
    for (i = NumberToSplit1; i < (NumberToSplit1+NumberToSplit2); i++) {
        printf("%c", getStr[i]);
    }
    printf("\n");
    for (i = (NumberToSplit1+NumberToSplit2); i < (NumberToSplit1+NumberToSplit2+NumberToSplit3); i++) {
        printf("%c", getStr[i]);
    }

    //If you want to save it in an integer, you can use strcat function to save the temp 2 numbers in a string convert that to integer

    //or use http://stackoverflow.com/questions/7021725/converting-string-to-integer-c

    printf("\n");
    printf("\n");

}

Output:

Enter a number: 12345
How many number splits you want: 2 2 4

    2-2-4 split: 
    12
    34
    5

Program ended with exit code: 0
Gravity Mass
  • 605
  • 7
  • 13
1

Otherwise, first, to convert the int to a string:

 #include <stdio.h>
 int n = 12345678;
 int len = snprintf(NULL, NULL, "%d", n);
 char *digits = malloc(len);
 sprintf(digits, "%d", n);

Then you could split the string up various ways, such as:

 int a, b, c;
 sscanf(digits, "%2d%2d%4d", &a, &b, &c);

Or:

 char sa[2], sb[2], sc[4];
 char *cp = digits;
 sa[0] = *cp++;
 sa[1] = *cp++;
 sb[0] = *cp++;
 sb[1] = *cp++;
 sc[0] = *cp++;
 sc[1] = *cp++;
 sc[2] = *cp++;
 sc[3] = *cp++;

 printf("%2c %2c %4c\n", sa, sb, sc);

Or:

 // Create 3 buffers to hold null-terminated ('\0' terminated) strings
 char sa[3] = { 0 } , sb[3] = { 0 }, sc[4] = { 0 };

 char *cp = digits;
 sa[0] = *cp++;
 sa[1] = *cp++;
 sb[0] = *cp++;
 sb[1] = *cp++;
 sc[0] = *cp++;
 sc[1] = *cp++;
 sc[2] = *cp++;
 sc[3] = *cp++;

 printf("%s %s %s\n", sa, sb, sc);

Then free your memory:

 free(digits);

Etc... Etc... Etc...

clearlight
  • 12,255
  • 11
  • 57
  • 75
1

Since you want to split an eight digit number in a 2-2-4 shape you can just use integer division and modulo.
Assuming you don't want the negative sign if any :

void split( int input, int output[3] )
{
  if ( input<0 )
    input = -input;
  output[0] = input % 10000;
  output[1] = ( input / 10000 ) % 100;
  output[2] = input / 1000000;
}
Anonymous Coward
  • 3,140
  • 22
  • 39
  • Would something like this work as well? - 'int main(void) { int a, m, d, y; printf("Please input an integer value: "); scanf("%d", &a); m= a/1000000; d=(a-m)/10000; y=(a-d-m); printf(“The date is %d/%d/%d\n”, m, d, y); }' – Mukul Ram Oct 04 '15 at 20:42
  • You said it is positional in the comments, not arithmetic. For positional stuff, since there is only one case, arithmetic is weird. You need to be very clear and specific so people don't waste their time going down the wrong path. I usually solve for the general case, but since this is just an educational exercise with no practical value it doesn't address why you're splitting the number on those boundaries, where you get the data, or if there will be other cases, or if the positions and field sizes change. So many ways to approach that and solve it. All the context needs to be provided. – clearlight Oct 04 '15 at 20:45
  • @nerdistcolony Sorry about that. It is supposed to be positional. This math works to get the 2-2-4 combination. Basically I'm solving random exercises. In this current one, I need to - 1) Prompt the user for an integer 2) Take in an integer and attribute its value to a variable 3) Split the integer for example 12345678 into three different integers 12, 34 and 5678. 4) Then there's some math calculations related to converting those into mm/dd/yyyy and figuring out which day the date corresponds to, but that isn't what I need help with. My problem is that I don't know the basic syntax. – Mukul Ram Oct 04 '15 at 20:50
  • @nerdistcolony I could write the program in Java and post it if that helps to give everyone a clear idea of what it's supposed to do. Would that be recommended? – Mukul Ram Oct 04 '15 at 20:51
  • @MukulRam - it wouldn't hurt to show what you have to clarify it, but someone has probably given you the info you need by now. Have you used Swift for iOS yet? Become an Apple app developer. You'll be happy :-) – clearlight Oct 04 '15 at 20:54
  • @nerdistcolony I haven't yet. I'm just starting out as a Freshman at College and wanted to get a feel of as many languages as I could. Would you recommend it? – Mukul Ram Oct 04 '15 at 21:00
  • @Mukul. No, it would not work. Just compile and execute that and you will see for yourself. Though your idea is not bad, you just need to make a few adjustments to your code. – Anonymous Coward Oct 04 '15 at 21:21
  • @JoseAntonioDuraOlmos I got my mistake. Thanks. – Mukul Ram Oct 04 '15 at 21:32