List of all available Websheets
Viewing cpp/pointers/toi by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Write a function <tt>ctoi</tt> that converts a character to an integer, e.g. <tt>ctoi('7')</tt> gives the integer 7. Then write <tt>atoi</tt>, that converts a C string to an integer. <p> You don't need to worry about negative numbers, overflow, or input validation for this exercise. |
Public permissions | |
Engine | |
Template / Reference solution |
int ctoi(char ch) { int ch_as_int = (int)ch; \[ int zero_as_int = '0'; return ch_as_int - zero_as_int; ]\ } int atoi(char* str) { \[ int value = 0; int i = 0; ]\ while (\[str[i] != '\0']\) { // iterate through C string // multiply by 10 and add next ctoi each time \[ value *= 10; value += ctoi(str[i]); i++; ]\ } \[ return value; ]\ } |
C++ test suite See manual | [ ["check-function", "ctoi", "int", ["char"]], ["call-function", "ctoi", ["'7'"]], ["call-function", "ctoi", ["'0'"]], ["call-function", "ctoi", ["'1'"]], ["check-function", "atoi", "int", ["char*"]], ["call-function", "atoi", ["\"48\""]], ["call-function", "atoi", ["\"103\""]], ["call-function", "atoi", ["\"2014\""]], ["call-function", "atoi", ["\"2\""]] ] |
Solution visibility |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.