aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsinanmohd <pcmsinan@gmail.com>2022-06-04 18:57:14 +0530
committersinanmohd <pcmsinan@gmail.com>2022-06-04 18:57:14 +0530
commitef4f5e3614fc9afbf4dd15c44d59d9535f43b290 (patch)
treed5f6d95c5ce2f9050a965175d887074a73e007ff
parent33f3e3f2bdd7e4eaf89e9708505d3c0d27fc23f7 (diff)
add 3.5
-rw-r--r--3.4.c3
-rw-r--r--3.5.c62
2 files changed, 64 insertions, 1 deletions
diff --git a/3.4.c b/3.4.c
index d4a1c9a..5c75d03 100644
--- a/3.4.c
+++ b/3.4.c
@@ -38,7 +38,7 @@ itoa(int n, char str[])
{
int sign, i;
- /* to include the most -ve int*/
+ /* to include the most -ve int */
n = ((sign = n) < 0) ? -(n+1) : n-1;
i = 0;
@@ -49,6 +49,7 @@ itoa(int n, char str[])
if (sign <0)
str[i++] = '-';
+ /* part of "to include the most -ve int" */
str[0] += 1;
str[i] = '\0';
diff --git a/3.5.c b/3.5.c
new file mode 100644
index 0000000..e7b4d3e
--- /dev/null
+++ b/3.5.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+
+#define MAXLEN 15
+
+void reverse(char str[]);
+char* itob(int n, char str[], int base);
+
+int
+main(void)
+{
+ int n = 21474836;
+ char str[MAXLEN];
+
+ printf("%x\n", n);
+ printf("%s\n", itob(n, str, 16));
+
+ 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*
+itob(int n, char str[], int base)
+{
+ int sign, i;
+
+ /* to include the most -ve int */
+ n = ((sign = n) < 0) ? -(n+1) : n-1;
+
+ i = 0;
+ do {
+ if (n%base >= 0 && n%base <= 9)
+ str[i++] = n%base + '0';
+ else
+ str[i++] = n%base + 'a' - 10;
+ } while ((n /= base) > 0);
+
+ if (sign <0)
+ str[i++] = '-';
+
+ /* part of "to include the most -ve int" */
+ str[0] += 1;
+ str[i] = '\0';
+
+ reverse(str);
+
+ return str;
+}