Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Convert String to Integer

Medium Acceptance 60% Points 30

You are given a string s that represents a valid non-negative integer. Your task is to convert the string into its corresponding integer value. You are not allowed to use built-in conversion functions such as stoi(), parseInt(), or similar methods. Return the integer value.

Examples
Example 1

Example 1

Input: s = "1234"

Output: 1234

Explanation: The string is converted to its integer value.

Example 2

Example 2

Input: s = "0010"

Output: 10

Explanation: Leading zeros are ignored in the integer value.

Example 3

Example 3

Input: s = "7"

Output: 7

Explanation: A single digit string converts directly to the same integer.

Constraints
  • 1 = length of s = 105
  • The string contains digits only (0–9)
Topics
Recursion
Companies
Apple Samsung Media.net
Added 19 Jul 2026 60% acceptance
Ln 1, Col 1Sign in to save your code
123
123