Question: please write a c program that converts full string to number In convert.c, there is a function called btoi which takes two arguments, a character
please write a c program that converts full string to number In convert.c, there is a function called btoi which takes two arguments, a character array (for a string) and a numeric argument.The first argument is the string representation of a number in a base. The second argument is the numeric base. The return value for this function is going to by the numeric value for the string representation of the number. It's very like atoi in the class notes. If the number is invalid, like with atoi, we're going to return 0. Ignore leading spaces. The function should return once you reach the first invalid character in the string, just like atoi. This function should be able to make use of the digit_btoi function on each character in the string to save you some headaches. That way, you don't need to fumble with ASCII code manipulation in this part of the code.
The program is below , Please make sure you use the digit_itob function so that ascii conversions wont cause trouble
#include
#define MAX_LENGTH 100 #define INVALID_DIGIT -1
char digit_itob(int digit, int base) { if ( digit > base || digit < 0 || base < 0 ) return '\0'; if ( base < 11 ) { return '0'+digit; } else { if ( digit < 10 ) { return '0'+digit; } else { int tmp = digit%10; return 'A'+tmp; } } return '\0'; }
int btoi(char buffer[], int base) { /* insert your code here. */
return 0; }
void main() { char buffer[MAX_LENGTH]; char digit; int number, base; int argcount;
fprintf(stdout, "Test btoi. Format: "); fprintf(stdout, "> "); argcount = fscanf(stdin, "%s %d[ ]", buffer, &base); if (argcount < 2) { fprintf(stdout, "Skipping Test btoi. Malformed Input."); fgets(buffer, MAX_LENGTH, stdin); /* Clearing */ } else { fprintf(stdout, "Decimal Number Result: "); fprintf(stdout, "%d ", btoi(buffer, base)); } }
Expected Outputs
btoi("ZOOT", 36) = 1664957
btoi("HELLO", 36) = 29234652
btoi("2018", 10) = 2018
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
