All submissions

Convert String to Integer

Code

#include <stdio.h>
#include <stdint.h>
int is_digit(char a){
    if((a>=48) && (a<=57)){
        return 1;
    }
    return 0;
}
int custom_atoi(const char *str) {
    int i = 0,neg = 1,final_1 = 0,num =0 ;
    i = ((str[0] == 45) || (str[0] == 43));
    if(str[0] == 45){
        neg = -1;
    }
    while (is_digit(str[i])){
        num = (num*10) + (str[i++] - '0');
    }
    final_1 = num*neg;
    return final_1;
}

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;
}

Solving Approach

 

 

 

Loading...

Input

123abc

Expected Output

123