All submissions

Convert String to Integer

Code

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>

int custom_atoi(const char *str) {
    int number=0;
    char temp_digit[256];
    memset(temp_digit,0,sizeof(temp_digit));
    uint8_t temp_counter = 0;
    for(int i=0; str[i]!='\0'; i++)
    {
        if(isdigit(str[i]) || str[i] =='-' || str[i] =='+')
        {
            temp_digit[temp_counter++] = str[i];
        }

        else
        {
            break;
        }
    }

    temp_digit[temp_counter] = '\0';
    sscanf(temp_digit,"%d",&number);

    return number;
}

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