#include <stdio.h>
#include <stdint.h>
#define NUM_ZERO ('0')
#define NUM_NINE ('9')
#define SIGN_MINUS ('-')
#define SIGN_PLUS ('+')
#define IS_NUMERIC(chr) (((chr>=NUM_ZERO) && (chr<=NUM_NINE))?1:0)
#define IS_SIGN_MINUS(chr) ((chr==SIGN_MINUS)?1:0)
#define IS_SIGN_PLUS(chr) ((chr==SIGN_PLUS)?1:0)
#define CHAR2NUM(chr) (chr - NUM_ZERO)
int custom_atoi(const char *str) {
// Your logic here
int num = 0;
int sign = 1;
if(IS_SIGN_MINUS(*str))
{
sign = -1;
str++;
}
else if(IS_SIGN_PLUS(*str))
{
sign = 1;
str++;
}
while (*str && IS_NUMERIC(*str)) {
num = num * 10 + CHAR2NUM(*str);
str++;
}
return sign*num;
}
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