Given a long integer, the task is to convert it into its equivalent string representation in C++.
- The conversion can be performed manually by processing individual digits.
- C++ also provides stringstream and std::to_string() for simpler conversions.
Examples:
Input: N = -10243213
Output: -10243213
Input: N = 42131983
Output: 42131983
Approaches to Convert Long to String in C++
The following approaches can be used:
1. Using a Custom Conversion Function
The number can be converted digit by digit. Since digits are extracted from right to left, a stack is used to restore their original order.
Steps:
- Handle the sign of the number.
- Extract the last digit using % 10.
- Convert the digit into a character by adding '0'.
- Store the characters in a stack.
- Pop the characters and append them to the result string.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
// Function to convert long long to string
string longToString(long long num)
{
// Handle zero separately
if (num == 0)
return "0";
bool isNegative = num < 0;
// Use unsigned arithmetic to safely handle LLONG_MIN
unsigned long long value =
isNegative ? -(unsigned long long)num
: (unsigned long long)num;
stack<char> digits;
// Extract digits from right to left
while (value > 0) {
digits.push('0' + value % 10);
value /= 10;
}
string result;
// Restore digits to their original order
while (!digits.empty()) {
result += digits.top();
digits.pop();
}
// Add the negative sign if required
if (isNegative)
result = "-" + result;
return result;
}
int main()
{
long long num = -10243213;
cout << longToString(num);
return 0;
}
Output
-10243213
Explanation: For num = -10243213:
- The negative sign is handled separately, and the digits are extracted from right to left using % 10.
- The digits are converted to characters, stored in a stack, and then popped to restore their original order before adding the negative sign.
2. Using stringstream
The stringstream class from the <sstream> header can convert a numeric value into a string by inserting the value into the stream and retrieving the resulting string using str().
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
long long num = 43543422;
stringstream stream;
// Insert the number into the stream
stream << num;
// Convert stream contents to a string
string result = stream.str();
cout << result;
return 0;
}
Output
43543422
Explanation
- A stringstream object is created.
- The long integer is inserted into the stream using <<.
- The str() function retrieves the stream contents as a string.
- The resulting string is printed.
3. Using std::to_string()
C++11 introduced std::to_string(), which provides a simple way to convert numeric values into strings.
#include <iostream>
#include <string>
using namespace std;
int main()
{
long long num = 76456474;
// Convert the number to a string
string result = to_string(num);
cout << result;
return 0;
}
Output
76456474
Explanation
- std::to_string() directly converts the numeric value into its string representation.
- It also handles negative values automatically.