#include <stdio.h>
#include <stdint.h>
int custom_atoi(const char *str) {
// Your logic here
int sign = 0;
int result = 0;
bool nbr = false;
while (*str){
if ((!(sign)) && ((*str == '-') || (*str == '+'))) { //determine sign
if (*str=='-') sign = -1;
else if (*str=='+') sign = 1;
}
else
if ((*str >= '0') && (*str <='9')) { //is number
result = result*10 + ((char)*str - '0');
nbr = true; //space not accepted anymore
if (!(sign)) sign = 1; //no +/- at start
}
else //not number
if ((*str==' ') && (!nbr)) {} //just ignore
else return (result * sign);
str++;
}
return (result * sign);
}
int main() {
char str[101];
fgets(str, sizeof(str), stdin);
// Remove newline
uint8_t i = 0;
while (str[i]) {
if (str[i] == '\n') {
str[i] = '\0';
break;
}
i++;
}
printf("%d", custom_atoi(str));
return 0;
}
Input
123abc
Expected Output
123