Created
May 27, 2017 11:07
-
-
Save Todai88/030fe62327cc164c9fa01db40f925a9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
itoa.c (i, s, w) | |
@param i: integer | |
@param s: char [] | |
@param w: width | |
@returns a string representation of i with any excess (left) padding | |
in blankspaces dependant on the width w. | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
#define MAXLINE 100 | |
unsigned numDigits(const unsigned n) { | |
if (n < 10) return 1; | |
return 1 + numDigits(n / 10); | |
} | |
void itoa(int number, char string_out [], int width) { | |
int i = width; | |
do { | |
string_out[i] = (number > 0) ? number % 10 + '0' : ' '; | |
number /= 10; | |
} while ((i--) != 0); | |
string_out[width + 1] = '\0'; | |
} | |
int main () { | |
char str [MAXLINE]; | |
if (numDigits(1030) > 10) { | |
printf("Sorry, the number you provided was longer than 10 characters\n"); | |
return 1; | |
} | |
itoa(1030, str, 10); | |
printf("Before: %d\nAfter: %s\n", 1030, str); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment