aboutsummaryrefslogtreecommitdiff
path: root/src/libc/string.c
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2021-04-09 10:29:23 +0200
committerLoic Guegan <manzerbredes@mailbox.org>2021-04-09 10:29:23 +0200
commitf6323421e22c3585c58b54658a11e1e4706cc8fa (patch)
treef317b7129dc4350fd088b59a43c1b6e80cd5c39b /src/libc/string.c
parent8fee35522dee033863f68c1d2b45f5fe988de9eb (diff)
Cleaning code and provide minimal libc
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