Javatpoint Logo
Javatpoint Logo

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.

Convert string to integer in C++

Different methods to convert the string data into integers in the C++ programming language.

  1. Using the stringstream class
  2. Using the stoi() function
  3. Using the atoi() function
  4. Using the sscanf() function

Using the stringstream class

The 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() function

A 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

  1. The sscanf() function belongs to the stringstream class, so we need to import the class into our program.
  2. Initialize a constant character string str.
  3. Create an integer variable to hold the converted string to integer values.
  4. Pass the string variable into the sscanf() function, and assign the sscanf() function to the integer variable to store the integer value generated by the function.
  5. Print the integer values.

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() function

The 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

  1. Initialize the string variable to store string values.
  2. After that, it creates an integer type variable that stores the conversion of string into integer type data using the stoi() function.
  3. Print the integer variable value.

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() function

An 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

  1. Initialize a pointer-type character array to store a string.
  2. After that, it creates an integer type variable that stores the conversion of string into integer type data using the atoi() function.
  3. Print the integer variable value.

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA