Topical Information

This lab will help you practice with C-strings and libraries. character manipulation will also be practiced.

Program Information

The compiler your employer has given you to work with comes with a slightly substandard library. It is missing the function atoi (as described in the book — see the index for directions; but really it just turns ASCII strings into an integer...).

Write such a function yourself. (I called mine str_to_long: '... str_to_long(const char str[], ...);'. You can fill in the ... parts, right?)

Place it in a library (I called mine strextra). Write a driver program for it.

Make sure all of the following cases work:

   "1234"    --> 1234
   "-123"    --> -123
   "   1"    -->    1
   "  -1"    -->   -1
   "  +1"    -->    1
   "+1.4"    -->    1
   "apes"    -->    undefined or 0
   "2 boxes" -->    2
   "12false" -->   12
   " -12.3 " -->  -12

Note the optional leading sign and the ignoring of leading whitespace and extra trailing information. You should "return" to the caller both the translated long integer value and the position where translation stopped. For the above cases, the translation stop position would be:

   "1234"    --> 4
   "-123"    --> 4
   "   1"    --> 4
   "  -1"    --> 4
   "  +1"    --> 4
   "+1.4"    --> 2
   "apes"    --> 0
   "2 boxes" --> 1
   "12false" --> 2
   " -12.3 " --> 4

Note: You are not allowed to use atoi, strtol, istringstream, or the like.

Thought Provoking Questions

  1. What argument(s) does your function need? Will you change the argument(s)? What type of value(s) (if any) does it return?

  2. How can you easily skip past the beginning whitespace? Is there a library function that might help detect that the characters within the string are whitespace characters? (Hint: It's not a string library function.)

  3. Is the translation of "-123" actually significantly different than that for "+123" or "123"? What one little thing must change?

  4. When translating "1" how can you access just that character '1'? What other character is in the string?

  5. When translating "1" how can you convert the character representation of the digit 1 to a numeric version (i.e. how can you make '1' become 1)? (Would type-casting help? Maybe a quick look at the ASCII chart in Appendix 3? Or even skimming through this function from another resource might help.)

  6. When translating "12", how can you ensure that you end up with 12 instead of 3? (i.e. You don't just add the digits together, right? Maybe you should look back at the notes on how cin's >> operator does it...)

  7. How can you stop and ignore the extra information in "+1.4"? (i.e. the ".4" is irrelevant to the translation of the integer value "+1" --> 1.) What tells you you are out of useful information? Is this different from the string "1 box"? How about "12false"? Is there a library function that might help detect what kind of character you are [about to be] dealing with? (Again, not a string function.)

  8. How can you tell the caller the position at which you stopped translating? (Hint: Is there a reason that return wasn't highlighted as a keyword above but rather put in quotes like that?)

  9. When the string contains no integer, how can you have the user's value be/remain undefined? (That is, in the same state that it entered your function..?)

  10. How can the user detect that their value hasn't been modified? (i.e. when their string didn't contain a valid integer, how can they tell?) (Hint: If you couldn't translate anything, what position did you tell them you stopped?)

  11. When translating a string to an integer, we can start from either the front or the end of the string. Discuss (briefly) the merits of each and conclude which is best.

  12. How do you protect your library from being circularly included?

  13. What changes are needed in your main application (the test application here) to get it to work with the library? What about the compiling process?

  14. How many files does your library consist of? What are they? Which one(s) do you #include?

This assignment is (Level 2).