C++ Program to Convert String to Integer Last Updated : 21 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Given a string of digits, the task is to convert the string to an integer. Examples: Input : str = "12345" Output : 12345 Input : str = "876538"; Output : 876538 Input : str = "0028"; Output : 28 CPP // C++ program to convert String into Integer #include <bits/stdc++.h> using namespace std; // function for converting string to integer int stringTointeger(string str) { int temp = 0; for (int i = 0; i < str.length(); i++) { // Since ASCII value of character from '0' // to '9' are contiguous. So if we subtract // '0' from ASCII value of a digit, we get // the integer value of the digit. temp = temp * 10 + (str[i] - '0'); } return temp; } // Driver code int main() { string str = "12345"; int num = stringTointeger(str); cout << num; return 0; } Output:12345 Time Complexity: O(|str|) Auxiliary Space: O(1) How to do using library functions? Please refer Converting Strings to Numbers in C/C++ for library methods. Comment More infoAdvertise with us Next Article C++ Program to Convert String to Integer T tusharupadhyay Follow Improve Article Tags : C++ cpp-string Practice Tags : CPP Similar Reads Convert String to size_t in C++ To convert String to size_t in C++ we will use stringstream, It associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). We must include the stream header file in order to use stringstream. When parsing input, the stringstream class comes in qu 1 min read Convert String to int in C++ Converting a string to int is one of the most frequently encountered tasks in C++. As both string and int are not in the same object hierarchy, we cannot perform implicit or explicit type casting as we can do in case of double to int or float to int conversion. Conversion is mostly done so that we c 7 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 Convert a floating point number to string in C Write a C function ftoa() that converts a given floating-point number or a double to a string. Use of standard library functions for direct conversion is not allowed. The following is prototype of ftoa(). The article provides insight of conversion of C double to string. ftoa(n, res, afterpoint) n -- 3 min read How to Convert Vector of int into String? In C++, there are numerous cases where a vector of integers is needs to be converted into a string. In this article, we will learn the different methods to convert the vector of int into string in C++.The simplest method to convert the vector of int into string is by creating a stringstream and addi 3 min read A creative C++ program to Zoom digits of an integer Write a C (or C++) program to ZOOM (magnify) the digits of an integer. It should take an integer from the user and display each digit of the integer in magnified form using some pattern. Examples: Input : 123 Output : @ @@ @ @ @@@@@ ------------------------------- @@@@ @ @ @ @ @@@@ ----------------- 4 min read Convert given Binary Array to String in C++ with Examples Given a binary array arr[] containing N integer elements, the task is to create a string s which contains all N elements at the same indices as they were in array arr[]. Example: Input: arr[] = {0, 1, 0, 1}Output: string = "0101" Input: arr[] = { 1, 1, 0, 0, 1, 1}Output: string = "110011" Different 6 min read std::to_string in C++ In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings.In this article, we will learn how to use std::to_string() in C++.Syntaxstd::to_strin 1 min read Extract all integers from string in C++ Given a string, extract all integers words from it. Examples : Input : str = "geeksforgeeks 12 13 practice" Output : 12 13 Input : str = "1: Prakhar Agrawal, 2: Manish Kumar Rai, 3: Rishabh Gupta" Output : 1 2 3 Input : str = "Ankit sleeps at 4 am." Output : 4 The idea is to use stringstream:, objec 2 min read Remove Leading Zeros From String in C++ Given a string of digits, remove leading zeros from it. Examples: Input : 00000123569 Output : 123569 Input : 000012356090 Output : 12356090 In this article, we will use two string functions i.e, string erase and stoi() to remove leading zeros from the string. 1. Using String Erase FunctionCount tra 2 min read Like