aboutsummaryrefslogtreecommitdiff
path: root/5.6.2.c
diff options
context:
space:
mode:
authorsinanmohd <pcmsinan@gmail.com>2022-06-18 16:13:44 +0530
committersinanmohd <pcmsinan@gmail.com>2022-06-18 16:13:44 +0530
commitff4846af7dea497d75af09acfa6c2c8ca48fd67e (patch)
tree974ad1b5f260c398405c04578b90155480250aa5 /5.6.2.c
parentc55c5080200f0f31ce05485af7cce4cf4204ac40 (diff)
5.6
Diffstat (limited to '5.6.2.c')
-rw-r--r--5.6.2.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/5.6.2.c b/5.6.2.c
new file mode 100644
index 0000000..d29afc6
--- /dev/null
+++ b/5.6.2.c
@@ -0,0 +1,61 @@
+#include <stdio.h>
+
+#define MAXLEN 15
+
+void reverse(char str[]);
+char* itoa(int n, char str[]);
+
+int
+main(void)
+{
+ int n = -147483647;
+ char str[MAXLEN];
+
+ printf("%d\n", n);
+ printf("%s\n", itoa(n, str));
+
+ return 0;
+}
+
+void
+reverse(char str[])
+{
+ char temp, *str_og;
+
+ str_og = str;
+
+ while (*str++)
+ ;
+ str -= 2;
+
+ while (str_og < str) {
+ temp = *str_og;
+ *str_og++ = *str;
+ *str-- = temp;
+ }
+}
+
+char*
+itoa(int n, char str[])
+{
+ int sign;
+ char *str_og = str;
+
+ /* to include the most -ve int */
+ n = ((sign = n) < 0) ? -(n+1) : n-1;
+
+ do {
+ *str++ = n%10 + '0';
+ } while ((n /= 10) > 0);
+
+ if (sign <0)
+ *str++ = '-';
+
+ /* part of "to include the most -ve int" */
+ *str_og += 1;
+ *str = '\0';
+
+ reverse(str_og);
+
+ return str_og;
+}