Convert string to integer in C++This section will discuss the different methods to convert the given string data into an integer using the C++ programming language. There are some situations or instances where we need to convert a certain data to another type, and one such situation is to convert string to int data in programming. For example, we have a numeric string as "143", and we want to convert it to a numeric type. We need to use a function that converts a string to an integer and returns the numeric data as 143. Now, we will learn each method that helps convert string data into integers in the C++ programming language. Different methods to convert the string data into integers in the C++ programming language.
Using the stringstream classThe stringstream is a class used to convert a numeric string to an int type. The stringstream class declares a stream object to insert a string as a stream object and then extracts the converted integer data based on the streams. The stringstream class has "<<" and ">>" operators, which are used to fetch data from (<<) operator and insert data by passing stream to (>>) left operator. Let's create a program to demonstrate the stringstream class for converting the string data into an integer in the C++ programming language. Program1.cpp Output The string value is: 143 The representation of the string to integer type data is: 143 In the above program, we use the stringstream class to create an obj object, and it helps to convert string data into an integer. We then use the "<<" operator to insert string characters into the obj object, and then we use the ">>" operator to extract the converted string from obj into numeric data. Using the sscanf() functionA sscanf() function converts a given string into a specified data type like an integer. Syntax The sscanf() function has three arguments to specify the char string (str), data specifier (%d), and the integer variable (&intvar) to store the converted string. Algorithm of the sscanf() function
Let's consider an example to use the sscanf() function to convert the string into numeric number in C++. Program2.cpp Output The value of the character string is: 555 The representation of string to int value of numdata is: 555 The value of the character string is: 143 The representation of string to int value of numdata is: 143 The value of the character string is: 101 The representation of string to int value of numdata is: 101 Using the stoi() functionThe stoi() function converts a string data to an integer type by passing the string as a parameter to return an integer value. Syntax The stoi() function contains an str argument. The str string is passed inside the stoi() function to convert string data into an integer value. Algorithm of the stoi() function
Let's create a program to use the stoi() function to convert the string value into the integer type in the C++ programming language. Program3.cpp Output The conversion of string to an integer using stoi("108") is 108 The conversion of string to an integer using stoi("56.78") is 56 The conversion of string to an integer using stoi("578 Welcome") is 578 Using the atoi() functionAn atoi() function is used to convert the character string into an integer value. An atoi() function passes the character type string to return the integer data. Syntax Algorithm of the atoi() function
Let's create a program to use the atoi() function to convert the string value into the integer type in the C++ programming language. Program4.cpp Output The conversion of string to an integer value using atoi("256") is 256 The conversion of string to an integer value using atoi("16.18") is 16 The conversion of string to an integer value using atoi("1088 Good Bye") is 1088 Next TopicDelete Operator in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India