Write the function htoi(s) , which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F .

Is this a good solution? Are all cases covered in this code? I did take care of variable overflow aswell.

Is this how they intended for this function to look like?

Code:#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <ctype.h> unsigned int htoi(char s[]) { unsigned int val = 0; int x = 0; if(s[x] == '0' && (s[x+1]=='x' || s[x+1]=='X')) x+=2; while(s[x]!='\0') { if(val > UINT_MAX) return 0; else if(s[x] >= '0' && s[x] <='9') { val = val * 16 + s[x] - '0'; } else if(s[x]>='A' && s[x] <='F') { val = val * 16 + s[x] - 'A' + 10; } else if(s[x]>='a' && s[x] <='f') { val = val * 16 + s[x] - 'a' + 10; } else return 0; x++; } return val; } int main(int argc, char *argv[]) { char hexalpha[] = "0xFFF"; char test[] = "adfa"; if(htoi(hexalpha)==0) printf("\nHex string overflow or not a hex character.\n"); else printf("%u\n", htoi(hexalpha)); getchar(); return 0; }