Information
Before writing any code, you should write down the steps (in English or your native language) you need to take to solve these problems. This is your pseudocode. Once you have your pseudocode written, you can convert it into C code. If you can't write down the steps in English, you certainly can't write it in C.
The function you'll be writing today deals with NUL-terminated C-style strings. The only header file you can include is string.h, and you'll only need the strlen function.
The function you will write will convert a string of digits into an integer. It is sort of like the C standard library function atoi. The name of the function is strtoint (string to integer) and the prototype is:
You will be given a valid NUL-terminated C-style string as the only parameter and will return the integer value of the string. The string will be a valid integer representation and may be negative. The length of the string is not provided because you will use the strlen function to get the length.int strtoint(const char string[]);
Some valid strings are:
There will be no leading + (plus sign) on positive integers and no spaces in the strings. The strings will only contain the digits 0 through 9 and an optional leading minus sign. You can safely assume that the integers you are creating will fit in a 32-bit signed int."0" "1" "123" "1234567890" "-1" "-0" "-1234567890"
You must not use the pow() function. (I have demonstrated why that is a bad idea.) You may write your own integer power function, if you need one, but it's not required. (I didn't need it.)
Hint: Work on getting positive numbers working and then try to get the negative numbers working. Like all problems, there are several ways that you can solve this. One way is to recognize the pattern in base 10 numbers:
Using pointers or subscripts (your choice), you can extract the '4', '3', '2', and '1' characters from the string, convert them to digits (e.g. '4' - '0' == 4), and then multiply by the correct power of ten.1234 = 4 + 30 + 200 + 1000 = (4 * 1) + (3 * 10) + (2 * 100) + (1 * 1000) = (4 * 100) + (3 * 101) + (2 * 102) + (1 * 103)
Another way is to work left to right, extracting the current character and multiplying by 10 and adding the next character, multiplying by ten, adding the next, multiplying by 10, etc. like this:
You'll have to do some slight additional work to handle negative numbers.1234 = ((((1 * 10) + 2) * 10) + 3) * 10) + 4
Approximate number of lines of code: 15.
Files:
Put the function in a C file named strtoint.c. The command line to build the program is:
This is a partial strtoint.c with functions to get you started.gcc -O -Werror -Wall -Wextra -ansi -pedantic main.c strtoint.c -o strtoint
|
Output for all of the sample tests
Notes
strlen returns the length of the string, which is the number of characters in the string. It does not include the terminating NUL byte in the count. The type size_t is basically an unsigned integer type that you can use, if needed.size_t strlen(const char *string);
For those that would like an additional challenge:
into the float value 3.14159."3.14159"