Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The strlib.h interface exports the following methods for converting between inte

ID: 3637198 • Letter: T

Question

The strlib.h interface exports the following methods for converting between integers and strings: string integerToString(int n); int stringToInteger(string str); The first function converts an integer into its representation as a string of decimal digits, so that, for example. integerToString(l729) should return the string "1729". The second converts in the opposite direction so that calling stringToInteger("-42") should return the integer -42. Your job ill this problem is to Write the functions intToString and stringToInt (the names have been shortened to avoid having your implementation conflict with the library version) that do the same thing as their strlib.h counterparts but use a recursive implementation. Fortunately, these functions have a natural recursive structure because it is easy to break an integer down into two components using division by 10. This decomposition is discussed on page 42 in the discussion of the digitsum function. The integer 1729. for example, breaks down into two pieces, as follows: If you use recursion to convert the first part to a string and then append the character value corresponding to the final digit, you will get the string representing the integer as a whole. As you work through this problem, you should keep the following points in mind: Your solution should operate recursively and should use no iterative constructs such as for 01 while. It is also inappropriate to call the provided integerToString function or any other library' function that does numeric conversion The value that you get when you compute n % 10 is an integer, and not a character. To convert this integer to its character equivalent you have to add the ASCII code for the character 'O' and then cast that value to a char. If you then need to convert that character to a one-character string, you can concatenate it with string(). (The Java trick of concatenating with doesn't work because string constants are C-style strings.) You should think carefully about what the simple cases need to be. In particular, you should make sure that calling intToString(0) returns "0" and not the empty string. Tins fact may require you to add special code to handle this case. Your implementation should allow n to be negative, as illustrated by the earlier example in which stringToint("-42") returns -42. Again, implementing these functions for negative numbers will probably require adding special-case code.

Explanation / Answer

There are many options (as with everything in C++) 1. Use the standard conversion function stoi() (requires recent C++ compiler, such as MSVC 2010 or gcc 4.4) #include #include int main() { std::string s = "1234"; int i = stoi(s); std::cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote