String to Integer (atoi)

Practice this coding question on Coding Gurukul.

Problem statement

Parse a signed decimal integer from a text line. Skip leading ASCII spaces, then read at most one optional + or - sign. Consume consecutive decimal digits until the first other character or the end. Ignore the remaining suffix. If no digits are consumed, return 0. Clamp values below -2147483648 or above 2147483647 to those respective limits.

Input format

One complete line of text; preserve leading spaces. An empty line is allowed.

Output format

Print the parsed integer after clamping to the signed 32-bit range.

Constraints

0 <= line length <= 200; printable ASCII characters only.

Sample input

   -42 trailing

Sample output

-42

Sign in to run or submit a solution in C++, Java or Python.