aboutsummaryrefslogtreecommitdiff
path: root/src/libc/string.c
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2021-04-19 19:06:28 +0200
committerLoic Guegan <manzerbredes@mailbox.org>2021-04-19 19:06:28 +0200
commitca1e725b0dc9b10997897dd2ac6d44028601d9bb (patch)
tree0eb7bd087a2382d1d1a660ceda1eae01d1b8b3ca /src/libc/string.c
parentf5146ca9c987ed5e6ea69a0c67b7ed03444be30c (diff)
Init sources
Diffstat (limited to 'src/libc/string.c')
-rw-r--r--src/libc/string.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/libc/string.c b/src/libc/string.c
new file mode 100644
index 0000000..93a9e63
--- /dev/null
+++ b/src/libc/string.c
@@ -0,0 +1,29 @@
+#include "string.h"
+#include "math.h"
+
+void itoa(int i, char *a){
+ // Check if lower than 0
+ char neg=0;
+ if(i<0){
+ neg=1;
+ i=-i;
+ a[0]='-';
+ }
+
+ // Count number of digits
+ int len=1;
+ while(i/pow(10,len)>=1)
+ {
+ len++;
+ }
+
+ // Build string
+ int max_pow=len-1;
+ for(int j=0;j<=max_pow;j++){
+ int cur_pow=pow(10,max_pow-j);
+ char digit=i/cur_pow;
+ a[j+neg]='0'+digit;
+ i=i-digit*cur_pow; // Remove first digits (most significant)
+ }
+ a[len+neg]='\0';
+} \ No newline at end of file