atoi 2.0 func code

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

BIN
atoi_2.0 Executable file

Binary file not shown.

57
atoi_2.0.c Normal file
View File

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