String to Integer in Different Programming Languages Last Updated : 24 Aug, 2024 Comments Improve Suggest changes 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 String to Integer in Different Programming Languages K kartik Follow Improve Article Tags : Strings Python C Programs C++ Programs Java Programs C++ Python Programs PHP Programs DSA JavaScript Programs +6 More Practice Tags : CPPpythonStrings Similar Reads C++ Program For String to Long Conversion In this article, we will learn how to convert strings to long in C++. For this conversion, there are 3 ways as follows: Using stol()Using stoul()Using atol() Let's start by discussing each of these methods in detail. Example: Input: s1 = "20" s2 = "30" Output: s1 + s2 long: 50 1. Using stol() In C++ 3 min read Convert Octal String to Integer using stoi() Function in C++ STL The octal numeral system, or oct for short, is the base-8 number system and uses the digits 0 to 7, that is to say, 10 octal represents eight, and 100 octal represents sixty-four.How to Convert Octal String To Integer in C++?We can use an in-built library function stoi() which stands for String to I 2 min read C++ Program For String to Double Conversion There are situations, where we need to convert textual data into numerical values for various calculations. In this article, we will learn how to convert strings to double in C++. Methods to Convert String to Double We can convert String to Double in C++ using the following methods: Using stod() Fun 3 min read Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s 4 min read Convert String to int in C In C, we cannot directly perform numeric operations on a string representing a numeric value. We first need to convert the string to the integer type. In this article, we will discuss different ways to convert the numeric string to integer in C language.Example:Input: "1234"Output: 1234Explanation: 6 min read Like