Convert String to Integer

Code

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

int classify_number(char *str){
    int count = 0; 
    int number_ditmemay = 0; 
    int sign = 1;
    while(str[count] != '\0'){
        if((str[count] == '-')){
            sign = -1;
            count++;
        }
        else if((str[count] == '+')){
            sign = 1;
            count++; 
        }
        if((str[count] >= '0')&&(str[count] <= '9')){
            number_ditmemay = number_ditmemay*10 + (str[count] - '0'); 
        }
        else{
            break;
        }
        count++;
    }
    return number_ditmemay*sign;
}   
    

int main(){
    char str[100];
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0';
    printf("%d", classify_number(str));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

123abc

Expected Output

123