How would I go about taking a number like 123456
and having it print as 1 2 3 4 5 6
?
views:
383answers:
6The simplest way of doing this (though not the fastest) would probably be to first sprintf
the number to a string buffer, and then loop through the buffer printf
-ing one character and one space at a time.
There's no built-in way of doing this within the standard printf
formatting.
A common method would be to extract each digit, and then print that digit. I won't give you the code, but it's the implemented version of:
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 */
This will be backwards. I'll leave it as an exercise to you to fix that. (Hint: In the loop, put the result into an array of characters, and when you're finished start at the last index of the array and print backwards.)
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');
As 'jamesdlin' has mentioned in his comment, GMan's approach would work, however you will need to store it in a buffer in order to print out in the correct order (his algorithm would print out "6 5 4 3 2 1" for input 123456). At this point I'd say that it would be much simpler to just use sprintf as 'therefromhere' suggested in his answer (if this is not an algorithm class assignment of course).
In my opinion the simplest way to do it would be using recursion, this way you can print out digits in the right order without using buffers.
The 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;
}
Input:
-78900456
Output:
- 7 8 9 0 0 4 5 6
EDIT: Thanks to Steve Jessop who suggested a correct algorithm for positive integers while I was away. I changed the above method to print out correctly for all ints (positive and negative), without the last space.
Please note that we can avoid checking for negative values in every recursion, by doing the check just once (in the main function or wherever) but I didn't write it because we would lose more on clarity than gain in performance.
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++]);
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;
}