Home c++ ATOI Sales

ATOI Sales

Author

Date

Category

You need to implement the ATOI feature so that it converts a number with several discharges.

int atoi (String Str) {
   int x = 0;
   For (int i = 0; i & lt; size (STR); I ++) {
    x = int (str [i]) - int ('0');
   }
   Return x;
  }

I did so, only it will be for me to translate each number (I enter ‘125’, and it will bring me 1, 2, and then 5, i.e. separately)
I need 100, then 20 and finally 5
Help please


Answer 1, Authority 100%

Possible implementation:

# include & lt; cctype & gt;
INT ATOI (Const Char * Str)
{
 int number = 0;
 INT SIGN = 1;
 if (* Str == '+' || * str == '-') {
  if (* str == '-') {
   SIGN = -1;
  }
  ++ str;
 }
 While (STD :: ISDIGIT (* STR)) {
  Number = Number * 10 + (* Str - '0');
  ++ str;
 }
 RETURN NUMBER * SIGN;
}

Input string Proper to be zero-terminated, the algorithm is simple: We take a line if the next digit symbol then multiply our final number to 10 and add this figure to the number otherwise leave the cycle and return the number. Behavior In case of error, you need to determine under your needs, it returns zero in this implementation if the string is not numbered from the very beginning

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions