C++ Program For String to Long Conversion
Last Updated :
03 May, 2023
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++, the stol() function performs a string to long conversion.
Syntax:
long int stol (const string& str, size_t* idx = 0, int base = 10)
Parameters:
- str: It specifies a string object with the representation of an integral number.
- idx: It specifies a Pointer to an object of type size_t, whose value is set by the function to the position of the next character in str after the numerical value. The parameter can also be a null pointer, in which case it is not used.
- base: It specifies the numerical base to determine the number system in which the characters are interpreted. If the base is 0, the base to be used is determined by the format in the sequence. The default value is 10.
Below is the C++ program to string to long using stol():
C++
// C++ program to convert
// string to long using stol()
#include <iostream>
using namespace std;
// Driver code
int main()
{
char s1[] = "20";
char s2[] = "30";
long int n1 = stol(s1);
long int n2 = stol(s2);
long int res = n1 + n2;
cout << res;
return 0;
}
The time complexity is O(1).
The auxiliary space is also O(1).
2. Using stoul()
In C++ to convert a string into a long integer, there is a function called stoul(). The stoul() function performs a string to unsigned long conversion.
Syntax:
unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
Parameters:
- str: String object with the representation of an integral number.
- idx: Pointer to an object of type size_t, whose value is set by the function to the position of the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used.
- base: Numerical base (radix) that determines the valid characters and their interpretation. If this is 0, the base used is determined by the format in the sequence. Notice that by default this argument is 10, not 0.
Below is the C++ program to convert string to long using stoul():
C++
// C++ program to convert
// string to long using stoul()
#include <iostream>
using namespace std;
// Driver code
int main()
{
char s1[] = "20";
char s2[] = "30";
unsigned long int n1 = stoul(s1);
unsigned long int n2 = stoul(s2);
unsigned long int res = n1 + n2;
cout << res;
return 0;
}
The time complexity is O(1)
The auxiliary space is also O(1)
3. Using atol()
In C++, the atol() function translates a string and returns its equivalent integer value.
Syntax:
long int atol (const char * str)
Parameters: The function accepts one mandatory parameter str which is the representation of an integral number.
Below is the C++ program to convert string to long using atol():
C++
// C++ program to convert
// string to long using atol()
#include <iostream>
using namespace std;
// Driver code
int main()
{
char s[] = "123456654";
long int n = atol(s);
cout << n;
return 0;
}
Similar Reads
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
C++ Program For Double to String Conversion Here, we will build a C++ program for double to string conversion using various methods i.e. Using to_stringUsing stringstreamUsing sprintfUsing lexical_cast We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 456321.7651234 Output: string: 456321.76
2 min read
C++ Program For char to int Conversion In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.Example:Input: '9'Output: 9Ex
2 min read
C++ Program For int to char Conversion In this article, we will learn how to convert int to char in C++. For this conversion, there are 5 ways as follows: Using typecasting.Using static_cast.Using sprintf().Using to_string() and c_str().Using stringstream. Let's start by discussing each of these methods in detail. Examples: Input: N = 65
6 min read
C++ Program to Perform Calculations in Pure Strings Given a string of operations containing three operands for each operation "type of command", "first operand", and "second operand". Calculate all the commands given in this string format. In other words, you will be given a pure string that will ask you to perform an operation and you have to perfor
5 min read
C Program to Extract Characters From a String Character extraction can be done by iterating through the string in the form of a character array. It basically means plucking out a certain amount of characters from an array or a string. Now, to take input in C we do it by using the following methods: scanf("%c",&str[i]); - Using a loopscanf("
2 min read
String C/C++ Programs C program to swap two StringsC Program to Sort an array of names or stringsC Program to Check if a Given String is PalindromeC/C++ Program for Return maximum occurring character in the input stringC/C++ Program for Remove all duplicates from the input string.C/C++ Program for Print all the duplicate
3 min read
C++ Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p
7 min read
Convert char* to std::string in C++ Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char*
3 min read
How to Convert wstring to int in C++? In C++, std::wstring is a type of string where each character is of a wide character type. These types of strings can be used to store the numerical strings which can then be converted to their corresponding type. In this article, we will see how to convert a wstring to an int in C++. Input: wstr =
2 min read