Home  >  Article  >  Backend Development  >  C++ program to convert int type variable to string

C++ program to convert int type variable to string

PHPz
PHPzforward
2023-09-03 22:05:033817browse

C++ program to convert int type variable to string

Integer type variables in C can store positive or negative integer values ​​within a predefined range. String variables can store sequences of letters, numbers, and special characters. There are many use cases where conversion from int to string is required. We discuss 3 different ways to convert an integer variable to a string.

If we talk about the algorithm, it is very simple. We get the input in an integer variable and convert it to a string variable.

Use to_string function

The easiest way to convert an integer value to a string in C is to use the to_string function. The to_string function is available by default; it accepts an integer value as input and provides a string value as output. Let’s look at the following example to understand this concept further.

grammar

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

algorithm

  • Get input in integer variable.
  • Use the to_string function to convert an integer value to a string and store it in a string variable.
  • Show results.

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 use the to_string function to convert an integer value to a string. One thing to note when displaying the output; we use the insertion operator (

Use ostringstream

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

grammar

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

algorithm

  • Get input in integer variable.
  • Pass the input integer variable to the ostringstream object.
  • Assign the string representation of the ostringstream object to the string output variable.
  • Show results.

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 added an integer value of 10 to the input value to indicate that it is an integer value, and added a string "10" to the output value to indicate that it is a string value.

Use sprintf

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

grammar

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

algorithm

  • Get input in integer variable.
  • Pass the input integer variable and character buffer to the sprintf function.
  • Assign the character buffer to the resulting string variable.
  • Show results.

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, the only difference is the conversion method. To use sprintf, we don't need to import any other libraries.

in conclusion

We may need to convert integers to strings on various occasions, mainly to output data from calculations using functions that only support string parameters. The first method we discussed is the simplest, but it is available starting from C 11 version. The second method using ostringstream requires importing another library sstream, while the last method using sprintf requires a auxiliary character or string buffer to convert the integer value to a string. The fastest way is the first, but if that doesn't work due to an outdated compiler, the other two should work.

The above is the detailed content of C++ program to convert int type variable to string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete