Question: Converts a string to an integer. Write the following function: int convertString2Integer(string str) Example #1: str: 123 return: 123 Example #2: str: -123 return: -123
Converts a string to an integer.
Write the following function:
int convertString2Integer(string str)
Example #1:
str: "123"
return: 123
Example #2:
str: "-123"
return: -123
Example #3:
str: "0000012"
return: 12
Constraints/Assumptions:
This problem WON'T require you to check for integer overflow. Your output is always assumed in-range (<= INT_MAX).
Your string input CANNOT be empty or NULL.
Your string input should ONLY contain '0' - '9' digit(s), and/or a negative sign ('-'); there are no other ASCII characters.
Your string input CAN have leading zeros, namely "01", "0000000000000124", etc; in which case your function should return 1, 124, etc.
You CANNOT use any string-to-integer library functions (namely atoi(), stoi(), stringstream(), etc.) for this problem. If you do, that is shortcut and you will get 2 logic points off this problem.
Grading for this problem is as follows:
Logic: 8 points (-1 point for each test case failure)
Styling/Documentation: 2 points (-1 for wrong function signature, and -1 for lack of documentation)
Submission:
You should have a total of ONE (1) cpp file for this problem
Follow the EXACT, SAME function signature; failure to abide by the exact function gets 1 point deduction from your logic!
Your CPP should have your main() and the named function above; no need for header files
There is no output needed. You can either leave your main() empty or have your own test cases similar to the following in your submission file. Bottomline is, your CPP file should compile and run!
/* int main() { cout << convertString2Integer("123"); cout << convertString2Integer("-999"); ... return 0; } */ Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
