aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsinanmohd <pcmsinan@gmail.com>2022-06-04 19:32:38 +0530
committersinanmohd <pcmsinan@gmail.com>2022-06-04 19:32:38 +0530
commitf690af3d466efd4d1ff45f0e79a0288be4f4c915 (patch)
tree7f646fc7330fceee985c8ff52ced3f116d921784
parentef4f5e3614fc9afbf4dd15c44d59d9535f43b290 (diff)
add 3.6.c
-rw-r--r--3.6.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/3.6.c b/3.6.c
new file mode 100644
index 0000000..3ecdb50
--- /dev/null
+++ b/3.6.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+
+#define MAXLEN 25
+
+void reverse(char str[]);
+char* itoa(int n, char str[], int fieldwidth);
+
+int
+main(void)
+{
+ int n = 214748;
+ char str[MAXLEN];
+
+ printf("%d\n", n);
+ printf("%s\n", itoa(n, str, 10));
+
+ 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 fieldwidth)
+{
+ int sign, i;
+
+ /* to include the most -ve int */
+ n = ((sign = n) < 0) ? -(n+1) : n-1;
+
+ i = 0;
+ do {
+ str[i++] = n%10 + '0';
+ } while ((n /= 10) > 0);
+
+ if (sign <0)
+ str[i++] = '-';
+
+ while (i < fieldwidth)
+ str[i++] = ' ';
+
+ /* part of "to include the most -ve int" */
+ str[0] += 1;
+ str[i] = '\0';
+
+ reverse(str);
+
+ return str;
+}