String to Integer in Different Programming Languages Last Updated : 24 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Below are example programs to do string to integer conversion in different programming languages. C++ #include <bits/stdc++.h> using namespace std; int main() { int val; char strn1[] = "12546"; val = atoi(strn1); cout << "String value = " << strn1 << endl; cout << "Integer value = " << val << endl; char strn2[] = "GeeksforGeeks"; val = atoi(strn2); cout << "String value = " << strn2 << endl; cout << "Integer value = " << val << endl; return (0); } // This code is contributed by shivanisinghss2110 C #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int val; char strn1[] = "12546"; val = atoi(strn1); printf("String value = %s\n", strn1); printf("Integer value = %d\n", val); char strn2[] = "GeeksforGeeks"; val = atoi(strn2); printf("String value = %s\n", strn2); printf("Integer value = %d\n", val); return (0); } Java import java.util.*; public class Main { public static void main(String[] args) { int val; String strn1 = "12546"; val = Integer.parseInt(strn1); System.out.println("String value = " + strn1); System.out.println("Integer value = " + val); String strn2 = "GeeksforGeeks"; try { val = Integer.parseInt(strn2); System.out.println("String value = " + strn2); System.out.println("Integer value = " + val); } catch (NumberFormatException e) { val = 0; System.out.println("String value = " + strn2); System.out.println("Integer value = " + val); } } } Python def main(): strn1 = "12546" val = int(strn1) print("String value = ", strn1) print("Integer value = ", val) strn2 = "GeeksforGeeks" try: val = int(strn2) except ValueError: val = 0 print("String value = ", strn2) print("Integer value = ", val) if __name__ == "__main__": main() C# using System; namespace GeeksforGeeks { class Program { static void Main(string[] args) { String strn1 = "12546"; int val = int.Parse(strn1); Console.WriteLine("String value = " + strn1); Console.WriteLine("Integer value = " + val); String strn2 = "GeeksforGeeks"; try { val = int.Parse(strn2); } catch (FormatException e) { val = 0; Console.WriteLine("String value = " + strn2); Console.WriteLine("Integer value = " + val); } } } } JavaScript // Javascript code to convert string to integer let val; let strn1 = "12546"; val = parseInt(strn1); console.log("String value = " + strn1); console.log("Integer value = " + val); let strn2 = "GeeksforGeeks"; val = parseInt(strn2); console.log("String value = " + strn2); console.log("Integer value = " + val); // This code is contributed by prasad264 OutputString value = 12546 Integer value = 12546 String value = GeeksforGeeks Integer value = 0 Complexity Analysis of Standard FunctionsBelow is a typical time complexity analysis of the library functionsTime Complexity: O(n), Only one traversal of the string is needed.Space Complexity: O(1), As no extra space is required.Detailed Articles for Different Languages:Below are detailed articles in different programming languages to do string to integer conversion.String to Integer in C/C++String to Integer in JavaString to Integer in JavaScriptString to Integer in PythonString to Integer in C#String to Integer in PHP How to implement your own conversion?We need to write a function that takes a string as an argument and returns its equivalent integer value. Please refer the below post for details.Write your own string to integer converter. Comment More infoAdvertise with us Next Article Integer literal in C/C++ (Prefixes and Suffixes) K kartik Follow Improve Article Tags : Projects JavaScript Programs Similar Reads Convert char to int (Characters to Integers) Converting Characters to Integers is essential in programming, whether it is users' input or extracting values from strings. Every programming language has different techniques to handle the conversion of these lines. In this article, we will explore how to convert characters to integers in several 4 min read Integer literal in C/C++ (Prefixes and Suffixes) Integer literal is a type of literal for an integer whose value is directly represented in source code. For example, in the assignment statement x = 1, the string 1 is an integer literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an integer literal indicating the valu 4 min read How to Convert String to Number Given a string representation of a numerical value, convert it into an actual numerical value. In this article, we will provide a detailed overview about different ways to convert string to number in different languages. Table of Content Convert String to Number in CConvert String to Number in C++Co 5 min read Difference between an Integer and int in Java with Examples In Java, int is a primitive data type while Integer is a Wrapper class.int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipul 6 min read Converting Number to String in C++ In C++, converting integers to strings or converting numbers to strings or vice-versa is actually a big paradigm shift in itself. In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. Let's look at som 4 min read Getting Started with Number Programs in Programming Numbers play a crucial role in programming, whether it's dealing with simple digits or diving into more complex concepts like number systems, arithmetic, series, sequences, random numbers, and special numerical values. This article is your guide to understanding how to code numbers in programming, u 2 min read Like