C++ Program to Convert int Type Variables into String


Integer type variables in C++ are able to store positive or negative integer values upto a predefined range. String variables can store a sequence of alphabets, numerals, and special characters. There are many use cases where the conversion from int to string is needed. We discuss 3 different methods to convert an integer variable into a string.

If we discuss the algorithm, it is pretty straightforward. We take input in an integer variable and then convert it into a string variable.

Using to_string function

The easiest way in C++ to convert an integer value to a string is using the to_string function. The to_string function is available by default; it takes an integer value as input and provides a string value as output. We take a look at the following example to understand the concept further.

Syntax

int ip = <integer value>;
string op = std::to_string(ip);

Algorithm

  • Take input in an integer variable.
  • Convert the integer value into a string and store it in a string variable using to_string function.
  • Display the result.

Example

#include <iostream> using namespace std; string solve(int ip) { //using the to_string function return to_string(ip); } int main() { int ip = 10; string op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " + op << endl; return 0; }

Output

The input value is: 10
The output value is: 10

In this example, we have used the to_string function to convert the integer value to string. A thing to note while we are displaying the ouput; we are using the insertion operator (<<) to display the integer value, but when we are displaying the string we are just concatenating the output value with the preceding string to demonstrate that it is a string.

Using ostringstream

ostringstream is a string buffer that contains a sequence of characters. In this method, we input the integer value into the ostringstream object and then format it into a string.

Syntax

int ip = <integer value>;
ostringstream oss;
oss << ip;
string op = oss.str();

Algorithm

  • Take input in an integer variable.
  • Pass the input integer variable to a ostringstream object.
  • Assign the string representation of the ostringstream object to the string output variable.
  • Display the result.

Example

#include <iostream> #include <sstream> using namespace std; string solve(int ip) { //using ostringstream ostringstream oss; oss << ip; return oss.str(); } int main() { int ip = 10; string op = solve(ip); cout<< "The input value after addition of 10 is: " << ip + 10 << endl; cout<< "The output value after addition of 10 is: " << op + "10" << endl; return 0; }

Output

The input value after addition of 10 is: 20
The output value after addition of 10 is: 1010

In the previous example, we have added an integer value 10 with the input value to demonstrate that it is an integer value and added a string ‘10’ with the output value to demonstrate that it is a string value.

Using sprintf

sprintf is a standard library function in C++ that sends formatted output to a string str. Using the sprintf function, we can convert an integer to a string.

Syntax

int ip = <integer value>;
char str[100];
sprintf(str, "%d", ip);
string s = str;

Algorithm

  • Take input in an integer variable.
  • Pass the input integer variable and a character buffer to the sprintf function.
  • Assign the character buffer to the resultant string variable.
  • Display the result.

Example

#include <iostream> using namespace std; string solve(int ip) { char str[100]; sprintf(str, "%d", ip); string s = str; return s; } int main() { int ip = 10; string op = solve(ip); cout<< "The input value after addition of 10 is: " << ip + 10 << endl; cout<< "The output value after addition of 10 is: " << op + "10" << endl; return 0; }

Output

The input value after addition of 10 is: 20
The output value after addition of 10 is: 1010

This example is similar to the previous example, only thing that is different is the conversion method. To use sprintf, we do not need to import any other libraries.

Conclusion

We may need to convert integer to string in various occasions, the major one being outputting data from a calculation using a function that supports only string arguments. The first method that we discussed is the easiest, but it is available from C++ 11 version. The second method that uses ostringstream requires another library sstream to be imported, and the final method using sprintf requires a secondary character or string buffer to convert the integer value to a string. The fastest method is the first one, but if that does not work due to the compiler being outdated, the other two methods should work.

Updated on: 19-Oct-2022

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements