diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /rushs/tinyprintf/my_itoa_base | |
add: graphs et rushs
Diffstat (limited to 'rushs/tinyprintf/my_itoa_base')
| -rw-r--r-- | rushs/tinyprintf/my_itoa_base/my_itoa_base.c | 49 | ||||
| -rw-r--r-- | rushs/tinyprintf/my_itoa_base/my_itoa_base.h | 6 |
2 files changed, 55 insertions, 0 deletions
diff --git a/rushs/tinyprintf/my_itoa_base/my_itoa_base.c b/rushs/tinyprintf/my_itoa_base/my_itoa_base.c new file mode 100644 index 0000000..29b3042 --- /dev/null +++ b/rushs/tinyprintf/my_itoa_base/my_itoa_base.c @@ -0,0 +1,49 @@ +#include "my_itoa_base.h" + +int base_count(const char *base) +{ + int i; + for (i = 0; base[i]; i++) + { + continue; + } + return i; +} + +char *my_itoa_base(int n, char *s, const char *base) +{ + if (n == 0) + { + s[0] = base[0]; + s[1] = '\0'; + return s; + } + char *head = s; + if (n < 0) + { + s[0] = '-'; + s++; + n = -n; + } + + // count numbers + int t = n; + int m = 0; + int b = base_count(base); + while (t > 0) + { + t /= b; + m++; + } + + // n = number count + s[m] = '\0'; + m--; + for (; m >= 0; m--) + { + s[m] = base[n % b]; + n /= b; + } + + return head; +} diff --git a/rushs/tinyprintf/my_itoa_base/my_itoa_base.h b/rushs/tinyprintf/my_itoa_base/my_itoa_base.h new file mode 100644 index 0000000..0be6314 --- /dev/null +++ b/rushs/tinyprintf/my_itoa_base/my_itoa_base.h @@ -0,0 +1,6 @@ +#ifndef MY_ITOA_BASE_H +#define MY_ITOA_BASE_H + +char *my_itoa_base(int n, char *s, const char *base); + +#endif /* ! MY_ITOA_BASE_H */ |
