<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p>

<p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p>

<ol>
	<li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li>
	<li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li>
	<li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li>
	<li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li>
</ol>

<p>Return the integer as the final result.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">42</span></p>

<p><strong>Explanation:</strong></p>

<pre>
The underlined characters are what is read in and the caret is the current reader position.
Step 1: &quot;42&quot; (no characters read because there is no leading whitespace)
         ^
Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
         ^
Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in)
           ^
</pre>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">-42</span></p>

<p><strong>Explanation:</strong></p>

<pre>
Step 1: &quot;<u>   </u>-042&quot; (leading whitespace is read and ignored)
            ^
Step 2: &quot;   <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative)
             ^
Step 3: &quot;   -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result)
               ^
</pre>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">1337</span></p>

<p><strong>Explanation:</strong></p>

<pre>
Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace)
         ^
Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
         ^
Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit)
             ^
</pre>
</div>

<p><strong class="example">Example 4:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">0</span></p>

<p><strong>Explanation:</strong></p>

<pre>
Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace)
         ^
Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
         ^
Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit)
          ^
</pre>
</div>

<p><strong class="example">Example 5:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">0</span></p>

<p><strong>Explanation:</strong></p>

<p>Reading stops at the first non-digit character &#39;w&#39;.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= s.length &lt;= 200</code></li>
	<li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li>
</ul>

class Solution {
public:
    string starttrim(const string &s) {
        size_t start = find_if_not(s.begin(), s.end(), [](unsigned char ch) { return isspace(ch); }) - s.begin();
        return s.substr(start);
    }

    string endtrim(const string &s) {
        size_t end = find_if_not(s.rbegin(), s.rend(), [](unsigned char ch) { return isspace(ch); }).base() - s.begin();
        return s.substr(0, end);
    }

    string trim(const string &s) {
        return endtrim(starttrim(s));
    }

    int myAtoi(string s) {
        s = trim(s);

        int len = s.length();
        int result = 0;
        int flag = 1;

        if (len == 0) return 0;

        int i = 0;

        // Handle sign
        if (s[i] == '+' || s[i] == '-') {
            flag = (s[i] == '-') ? -1 : 1;
            i++;
        }

        // Parse digits
        while (i < len && s[i] >= '0' && s[i] <= '9') {
            int digit = s[i] - '0';

            // Check for overflow
            if (result > (INT_MAX - digit) / 10) {
                return (flag == 1) ? INT_MAX : INT_MIN;
            }

            result = result * 10 + digit;
            i++;
        }

        return flag * result;
    }
};










// class Solution {
// public:
//     string starttrim(const string &s){
//         size_t start = find_if_not(s.begin(),s.end(), [](unsigned char
//         ch){return isspace(ch);}) - s.begin(); return s.substr(start);
//     }

//     string endtrim(const string &s){
//         size_t end = find_if_not(s.rbegin(),s.rend(),[](unsigned char
//         ch){return isspace(ch);}).base() - s.begin();

//         return s.substr(0,end);
//     }

//     string trim(string &s) {
//         return endtrim(starttrim(s));
//     }
//     int output(int &result){
//         int finaloutput = 0;
//         int val = 0;
//         while(result != 0){
//             val = result % 10;
//             finaloutput = finaloutput * 10 + val;
//             result /= 10;
//         }
//         return finaloutput;
//     }

//     int myAtoi(string s) {
//       s = trim(s);

//       int len = s.length();
//       int result = 0, val = 0;
//       int flag = 1;
//       char sign = '\0';

//       if (len == 0) return 0;

//       if(s[0] == '+' || s[0] == '-'){
//         flag = (s[0] == '+') ? 1: -1;
//       }

//       for(int i=0; i<len; i++){

//         if (result > (INT_MAX - val) / 10) {
//                 return (flag == 1) ? INT_MAX : INT_MIN;
//         }

//         else if(s[i] >='0' && s[i] <='9' ){
//             val = s[i] -'0';            
//             result = result * 10 + val;
//         }

//         else if((s[i] == '+' || s[i] == '-' || (s[i] >= 'a' && s[i] <= 'z')) && result >= 0){
//             return result;
//         }

//       }
//       result = output(result);
//       int result1 = output(result);
//       return flag * result1;
//     }
// };

9700