diff --git a/atoi_2.0 b/atoi_2.0 new file mode 100755 index 0000000..155f632 Binary files /dev/null and b/atoi_2.0 differ diff --git a/atoi_2.0.c b/atoi_2.0.c new file mode 100644 index 0000000..768fb44 --- /dev/null +++ b/atoi_2.0.c @@ -0,0 +1,57 @@ +#include +#include +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; +}