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++ program to demonstrate // long to string conversion // digit by digit #include <bits/stdc++.h> using namespace std;
string LongToString( long long_num)
{ stack< char > stringStack;
string signValue = "" ;
// if long number is negative store the negative sign to
// the signValue variable
if (long_num < 0) {
signValue = "-" ;
long_num = -long_num;
}
// while number is greater than 0, get last digit from it
// and convert it to character by adding '0' to it, and
// push to the stack.
while (long_num > 0) {
char convertedDigit = long_num % 10 + '0' ;
stringStack.push(convertedDigit);
long_num /= 10;
}
string long_to_string = "" ;
// while stack is not empty pop the character one by one
// and append to the resultant string.
while (!stringStack.empty()) {
long_to_string += stringStack.top();
stringStack.pop();
}
// return the resulatant string value by appending
// singValue to it.
return signValue + long_to_string;
} int main()
{ long long_num = -10243213;
string long_to_string = LongToString(long_num);
cout << long_to_string;
return 0;
} |
-10243213
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++ program to demonstrate // long to string conversion // using stringstream #include <iostream> #include <sstream> #include <string> using namespace std;
int main()
{ // initialize the long number
long long_num = 43543422;
// create a new object of stringstream class
stringstream stream;
// add long number to variable of type stringstream
stream << long_num;
string long_to_string;
// get string object from stringstream variable using
// str() method
long_to_string = stream.str();
cout << long_to_string;
return 0;
} |
43543422
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++ program to demonstrate // long to string conversion // using std::to_string methods #include <iostream> #include <string> using namespace std;
int main()
{ // initialize the long number
long long_num = 76456474;
string long_to_string;
// convert long to string using to_string() method
long_to_string = to_string(long_num);
cout << long_to_string;
return 0;
} |
76456474