-3
#include<stdio.h>

int isNumericChar(char s){
        return ((s > '0' && s <'9') ? 1:0);
}

int my_atoi(char* s){
        int res = 0;
        int sign = 1;
        while(*s != '\0'){
                if(isNumericChar(*s) == 0){
                        return 0;
                }
                if(*s == '-'){
                        sign = -1;
                        s++;
                }
                res = ((res * 10) + (*s - '0'));
                s++;
        }
        return (res*sign);
}

int main(){
        char *s = "924";
        printf("\natoi : %d\n",my_atoi(s));
        return 0;
}

How to convert the strings into integers for 0 and 9 entered ? Is there a way to convert the hexadecimal entered to be printed in hexadecimal representation ?

HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
Angus
  • 12,133
  • 29
  • 96
  • 151

1 Answers1

0

You can use printf("\natoi : %x\n",my_atoi(s)); according to this.

Note that this will work only for non-negative numbers.

For negative you can always use:

int my_number = my_atoi(s);
printf("\natoi : %s%x\n", (my_number < 0) ? "-" : "", my_number);

I don't really like this solution, but this is the only way I can think of right now.

Ivaylo Petrov
  • 1,162
  • 9
  • 18