All submissions

Convert String to Integer

Code

#include <stdio.h>
#include <stdint.h>
#include <ctype.h>
void custom_atoi(const char *str) {
    // Your logic here
    int count=0;
    while(*str!='\0'){
        int flag=0;
        if(isalpha(*str)){
            if(count==0){
                printf("0");
            }
            break;
        }
        else{
            if(*str=='-'){
                printf("%c",*str);
            }
            if(isdigit(*str)){
            printf("%c",*str);
            }
            count++;
        }
        str++;
    }
}

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

    custom_atoi(str);
    return 0;
}

Solving Approach

 

 

simple string pointer and array concepts

Loading...

Input

123abc

Expected Output

123