Convert Long to String in C++
Given a Long number N, the task is to convert the N to a string using C++.
Explanation:
Input: N = -10243213 // long input
Output: -10243213 // string output
Explanation: -10243213 is the string in the output.
Input: N = 42131983
Output: 42131983
Approach A
In this approach, we are converting every digit of the long to the char and appending it to the resultant string.
Follow the steps below to solve the problem:
1. If a long number is negative, store the negative sign to any variable and make the long number positive.
if (long_num < 0) {
signValue = "-";
long_num = -long_num; //converting number to positive value
}
2. Extract every digit from the long number one by one from the last and convert it to the character and push it to the stack.
while (long_num > 0) {
//add last digit to stack after converting it to char
stringStack.push(long_num % 10 + '0');
long_num /= 10; // remove last digit
}
3. While the stack becomes empty, pop one by one character from the string and append it to the resultant string.
while (!stringStack.empty()) {
// append top char of stack to string
long_to_string += stringStack.top();
stringStack.pop(); // pop char from the stack
}
4. Return the resultant string with the signValue.
return signValue + long_to_string;
Example:
C++
#include <bits/stdc++.h>
using namespace std;
string LongToString( long long_num)
{
stack< char > stringStack;
string signValue = "" ;
if (long_num < 0) {
signValue = "-" ;
long_num = -long_num;
}
while (long_num > 0) {
char convertedDigit = long_num % 10 + '0' ;
stringStack.push(convertedDigit);
long_num /= 10;
}
string long_to_string = "" ;
while (!stringStack.empty()) {
long_to_string += stringStack.top();
stringStack.pop();
}
return signValue + long_to_string;
}
int main()
{
long long_num = -10243213;
string long_to_string = LongToString(long_num);
cout << long_to_string;
return 0;
}
|
Time Complexity & Space Complexity: O (Digit_count_of_long_number)
Approach B
C++ contains the stringstream class inside the <stream> library. We can create an object of the stringstream class and insert the variable of any data type. It returns the string object.
- Create the object of stringstream class i.e. Stringstream stream;
- Add long number to stream object i.e. stream << 43543422;
- Get the string object from stream object using str() method i.e. long_to_string = stream.str();
Example:
C++
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
long long_num = 43543422;
stringstream stream;
stream << long_num;
string long_to_string;
long_to_string = stream.str();
cout << long_to_string;
return 0;
}
|
Approach C
The c++ <string> library provides the std::to_string() method to convert the any datatype to the string.
String long_to_string = to_string(76456474);
Example:
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
long long_num = 76456474;
string long_to_string;
long_to_string = to_string(long_num);
cout << long_to_string;
return 0;
}
|
Last Updated :
09 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...