diff options
Diffstat (limited to '3.4.c')
-rw-r--r-- | 3.4.c | 57 |
1 files changed, 57 insertions, 0 deletions
@@ -0,0 +1,57 @@ +#include <stdio.h> + +#define MAXLEN 15 + +void reverse(char str[]); +char* itoa(int n, char str[]); + +int +main(void) +{ + int n = -2147483648; + char str[MAXLEN]; + + printf("%d\n", n); + printf("%s\n", itoa(n, str)); + + return 0; +} + +void +reverse(char str[]) +{ + int i, j; + char temp; + + for (j = 0; str[j] != '\0'; j++) + ; + + for (i = 0, --j; i < j; i++, j--) { + temp = str[i]; + str[i] = str[j]; + str[j] = temp; + } +} + +char* +itoa(int n, char str[]) +{ + int sign, i; + + if ((sign = n) < 0) + n = -n; + + i = 0; + do { + str[i++] = n%10 + '0'; + } while ((n /= 10) > 0); + + if (sign <0) + str[i++] = '-'; + + str[i] = '\0'; + + reverse(str); + + return str; +} |