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

C++ program to convert int type variable to string

PHPz
Release: 2023-09-03 22:05:03
forward
3852 people have browsed it

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);
Copy after login

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;
}
Copy after login

Output

The input value is: 10
The output value is: 10
Copy after login

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 (<<) to display integer values, but when we display a string, we just concatenate the output value with the previous string to prove that it is a string. <<) 来显示整数值,但是当我们显示字符串时,我们只是将输出值与前面的字符串连接起来以证明它是一个字符串。

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();
Copy after login

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;
}
Copy after login

Output

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

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;
Copy after login

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;
}
Copy after login

Output

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

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template