String to Integer Conversion
Also known as: String to Integer (atoi)
Convert string to 32-bit signed integer Handle whitespace, sign, overflow Example: " -042" => -42
Convert a string s of at most 200 characters to a signed 32-bit integer, skipping leading space characters and then accepting at most one optional + or - sign. The input contains only English letters, ASCII digits, spaces, +, -, and decimal points. Read consecutive ASCII digits until the first non-digit or the end of the string, ignoring everything after that point. Return 0 if no digits are read, including for empty input or a sign without digits. Clamp values below -2147483648 to -2147483648 and values above 2147483647 to 2147483647.
Examples
You write
my_atoi(s: str)
Example 1
- Input
- s = "42"
- returns
- 42
Example 2
- Input
- s = " -042"
- returns
- -42
The pattern behind it
Strings
I reach for it when
- The input is text and the answer is a count, a substring or a new string.
- You need to compare, reverse or rebuild characters.
Say this before you type. Say which array pattern the string is hiding before you touch a character.
All 51 Strings problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.