Converting Number to String in C++
Converting numbers to string or vice-versa is actually a big paradigm shift in itself. In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. But lack of knowledge of certain essential tools binds us to do so. Some methods to achieve this task are mentioned in this article.
Converting Number to String in C++
There are 3 major methods to convert a number to a string, which are as follows:
- Using string Stream
- Using to_string()
- Using boost lexical cast
Method 1: Using string streams
In this method, a string stream declares a stream object which first inserts a number, as a stream into an object and then uses “str()” to follow the internal conversion of a number to a string.
Example:
CPP
// C++ code to demonstrate string stream method // to convert number to string. #include<iostream> #include <sstream> // for string streams #include <string> // for string using namespace std; int main() { int num = 2016; // declaring output string stream ostringstream str1; // Sending a number as a stream into output // string str1 << num; // the str() converts number into string string geek = str1.str(); // Displaying the string cout << "The newly formed string from number is : " ; cout << geek << endl; return 0; } |
The newly formed string from number is : 2016
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2: Using to_string()
The function to_string() accepts a number(which can be any data type) and returns the number in the desired string.
CPP
// C++ code to demonstrate "to_string()" method // to convert number to string. #include <iostream> #include <string> // for string and to_string() using namespace std; // Driver Code int main() { // Declaring integer int i_val = 20; // Declaring float float f_val = 30.50; // Conversion of int into string using // to_string() string stri = to_string(i_val); // Conversion of float into string using // to_string() string strf = to_string(f_val); // Displaying the converted strings cout << "The integer in string is : " ; cout << stri << endl; cout << "The float in string is : " ; cout << strf << endl; return 0; } |
The integer in string is : 20 The float in string is : 30.500000
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using boost lexical cast
Similar to string conversion, the ” lexical_cast() ” function remains the same, but in ‘boost lexical cast‘ time argument list modifies to “lexical_cast(numeric_var).
Example:
CPP
// C++ code to demonstrate "lexical_cast()" method // to convert number to string. #include <boost/lexical_cast.hpp> // for lexical_cast() #include <iostream> #include <string> // for string using namespace std; // Driver Code int main() { // Declaring float float f_val = 10.5; // Declaring int int i_val = 17; // lexical_cast() converts a float into string string strf = boost::lexical_cast<string>(f_val); // lexical_cast() converts a int into string string stri = boost::lexical_cast<string>(i_val); // Displaying string converted numbers cout << "The float value in string is : " ; cout << strf << endl; cout << "The int value in string is : " ; cout << stri << endl; return 0; } |
The float value in string is : 10.5 The int value in string is : 17
Method: Using the sprintf() function
C++
#include <iostream> using namespace std; int main() { int n=12234; char str[1000]; sprintf (str, "%d" , n); printf ( "the string is : %s" ,str); return 0; } |
the string is : 12234
Time Complexity: O(n)
Auxiliary Space: O(n)
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Please Login to comment...