Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

String to Integer (atoi)

Medium
stringsimulation

Implement atoi: skip leading spaces, read an optional sign, read digits until a non-digit, and clamp the result to the 32-bit signed range [−2³¹, 2³¹−1]. Anything unreadable yields 0.

Input: one line, the raw string. Output: the parsed integer.

Example 1
Input:
42
Output:
42
Example 2
Input:
   -042
Output:
-42
Example 3
Input:
1337c0d3
Output:
1337

Parsing stops at 'c'.

Example 4
Input:
words and 987
Output:
0

Constraints

  • 0 ≤ length ≤ 200
  • Letters, digits, spaces, '+', '-', '.'.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.