1
0
forked from KIX/lab1_git

myatoi func code

This commit is contained in:
KIX
2025-11-01 04:06:04 -04:00
parent ba2e7c99f4
commit 01f553fe4c
2 changed files with 34 additions and 0 deletions

BIN
atoi Executable file

Binary file not shown.

34
atoi.c Normal file
View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <string.h>
int myatoi(char *s, int l);
int main(int argc, char **argv)
{
printf("Print your string number\n");
char s[100]; scanf("%s", s);
printf("Your converted number is %d\n", myatoi(s, strlen(s)));
return 0;
}
int myatoi(char *s, int l)
{
int result = 0;
int is_pos = 1;
for(int i=0; i < l; i++)
{
if((int)s[i]==45)
{
is_pos = -1;
}
else
{
if((int)s[i] < 48 || (int)s[i] > 57)
{
printf("Not a number\n");
result = 0;
break;
}
result = result * 10 + ((int)s[i] - 48);
}
}
result = result * is_pos;
return result;
}