Question
Need help writing this in C int ascii2int ( int radix , int valueOfPrefix ) Convert a string to an integer value. The string comes
Need help writing this in C
int ascii2int | ( | int | radix, |
int | valueOfPrefix | ||
) |
Convert a string to an integer value. The string comes from the standard input. You must read one character at a time by using the built-in getchar() function which simply returns the next character available in the standard input. The end of the string is signaled by a newline character (' '). You MUST implement this function recursively. That means you shouldn't have loops. For information on the algorithm, see the Example revisited (page 3) on Sanjay's handout titled "Number Systems" (referenced in the main page). You may assume that the string is legal in the given radix (letters may be uppercase or lowercase). You can also assume that the string represents a non-negative number. Here's an example for how to test this function:
echo "48A6" | ./testConv a2i 12
This ensures that the string "48A6" is available for you to read from the standard input. The first time you call getchar(), it will return '4'. The second time, it will return '8', and so on. Eventually, it will return ' ' (because the echo command automatically appends a newline character).
This function should not print anything (you will lose all points for this function if it does). However, you may print stuff for your own debugging purposes as long as you remove any printing statements before you submit.
Parameters
radix | - the base you are working in (2-36) |
valueOfPrefix | - the integer value of the digits processed so far (the prefix). For example, if the string is "48A6" in base 12 and you're about to read 'A', the prefix is "48" and the integer value of this prefix is 56. When your function is called by the main program (the testConv.c file), this parameter is 0. |
Returns
the number represented by the string
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started