Atoi, is a Strings related problem and in this post we will see how we can solve this challenge in C++

Implement atoi to convert a string to an integer.

Example :

Input : "9 2704" Output : 9 Note: There might be multiple corner cases here. Clarify all your doubts using “See Expected Output”.

Questions: Q1. Does string contain whitespace characters before the number? A. Yes Q2. Can the string have garbage characters after the number? A. Yes. Ignore it. Q3. If no numeric character is found before encountering garbage characters, what should I do? A. Return 0. Q4. What if the integer overflows? A. Return INT_MAX if the number is positive, INT_MIN otherwise. first scan the string and seprate out the digits if any ignore spaces check if the current char is digit or not if it is garbage then return check if the number is negative or not take out the digits if the number is +ve for checking overflow for checking overflow for checking underflow for checking underflow

Please check the main.cpp snippet for the solution.

This solution originally posted at: Github by @susantabiswas