Insert spaces between numbers in C

How can I take a number like 123456 and print it as 1 2 3 4 5 6 ?

+4
source share
6 answers

As "jamesdlin" noted in his comment, the GMan approach will work, however you will need to save it in the buffer in order to print it in the correct order (its algorithm will print "6 5 4 3 2 1" for input 123456). At this point, I would say that it would be much easier to use sprintf as the “therefromhere” suggested in his answer (unless this is of course the purpose of the algorithm class, of course).

In my opinion, the easiest way to do this is to use recursion, so you can print the numbers in the correct order without using buffers.

Recursive implementation is very simple:

 void PrintfRecursivly(int number) { if (number < 0) { number *= -1; printf("- "); } if (number > 10) { PrintfRecursivly(number / 10); printf(" "); } printf("%d", number % 10); } int main() { int number = -78900456; PrintfRecursivly(number); return 0; } 

Entrance:

-78900456

Exit:

  - 7 8 9 0 0 4 5 6 

EDIT : thanks to Steve Jessop, who suggested the correct algorithm for positive integers while I was away. I modified the above method to print correctly for all ints (positive and negative), without the last space.

Please note that we can avoid checking negative values ​​in each recursion by performing the check only once (in the main function or anywhere), but I did not write it because we would lose more on clarity than on improving performance.

+7
source

The easiest way to do this (although not the fastest) would probably be the first sprintf number in the string buffer, and then pass through the printf buffer one character at a time and one space at a time.

There is no built-in way to do this in standard printf formatting.

+10
source

A common method is to extract each digit and then print that digit. I will not give you the code, but this is the implemented version:

 int d; // your number /* While `d` is not zero */ /* Modulus `d` with 10 to extract the last digit */ /* Print it, with your space */ /* Divide by 10 to remove the last digit */ /* Repeat */ 

It will be back. I will leave this an exercise for you to fix it. (Hint: in a loop, put the result in an array of characters, and when you finish starting with the last index of the array and print back.)

+2
source
 char buffer[50]; int myNum = 123456; int n; int i; n = snprintf(buffer, sizeof buffer, "%d", myNum); for (i = 0; i < n; i++) { putchar(buffer[i]); putchar(' '); } putchar('\n'); 
+2
source
 int number = 123456; char strNumber[64]; strNumber[0] = '\0'; sprintf_s(strNumber, "%d", number); int i = 0; while(strNumber[i] != '\0') printf("%c ", strNumber[i++]); 
0
source

This only works for unsigned integers:

 #include <stdio.h> #include <math.h> void print_number(unsigned int number) { int n = number, c = 0, p; while (n > 0) { n /= 10; c++; } for (n = c - 1; n >= 0; n--) { p = pow(10, n); printf("%d ", number / p); number -= number / p * p; } printf("\n"); } int main(int argc, char *argv[]) { print_number(1); print_number(12); print_number(123); print_number(1234); print_number(12345); print_number(1234567); print_number(12345678); print_number(123456789); return 0; } 
0
source

All Articles