> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/8-string-to-integer-atoi.md).

# 8 String to Integer (atoi)

Implementation to convert a string to an integer.

**Hint:**&#x43;arefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

**Notes:**&#x49;t is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

**Requirements for atoi:**

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT\_MAX (2147483647) or INT\_MIN (-2147483648) is returned.

```cpp
int myAtoi(string str) {

    // rid of spaces
    int integral_start = 0;
    while (str[integral_start] == ' ') integral_start++;

    // a sign or digit must come next
    if (!(isdigit(str[integral_start]) || str[integral_start] == '-' || str[integral_start] == '+'))
        return 0;

    // obtain sign (if any) - assume positive
    bool is_pos = str[integral_start] == '-' ? false : true;
    bool has_sign = str[integral_start] == '-' || str[integral_start] == '+';

    // must be a digit after sign
    if (has_sign) {
        integral_start++;
        if (integral_start >= str.size() || !(isdigit(str[integral_start]))) return false;
    }

    // obtain all digits that come next
    int integral_end = integral_start;
    while (integral_end < str.size() && isdigit(str[integral_end])) integral_end++;
    integral_end--;

    // form the result - everything after becomes discarded
    long long int result = 0;
    long long int base_mult = 1;

    for (int i = integral_end; i >= integral_start; i--) {
        result += (str[i] - '0') * base_mult;
        base_mult *= 10;
    }

    return is_pos ? result : -1 * result;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/8-string-to-integer-atoi.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
