How to use stringstream In C++
A stringstream connects a string object to a stream, allowing you to read from it as if it were a stream (like cin). Stringstream requires the inclusion of the sstream header file. The stringstream class comes in handy when processing input.
Below is the C++ program to convert int to char using stringstream:
C++
// C++ program to convert // int to char using // stringstream #include <iostream> using namespace std; #include <sstream> // Driver code int main() { int N = 1234; std::stringstream t; t << N; char const *n_char = t.str().c_str(); printf ( "n_char: %s \n" , n_char);; return 0; } |
n_char: 1234
Method: Converting int value to char by adding 0
C++
// C++ program to convert // int to char using typecasting by adding zero #include <iostream> using namespace std; //Driver code int main() { int number = 65; char charvalue = ( char (number)+0); cout << charvalue; return 0; } |
A
Time complexity: O(1).
Auxiliary space: O(1).
Approach: ASCII value offset approach
Steps:
- Take an integer input from the user.
- Check if the input value corresponds to a valid character in the ASCII table by checking the range of the input value.
- If the input value corresponds to a valid character, then add the corresponding offset value of ‘0’ or ‘A’ (depending on the input) to the integer value to get the corresponding character value.
- Output the corresponding character.
C++
#include <iostream> using namespace std; int main() { int num = 65; cout << "Enter an integer: " << num << endl; char ch; if (num >= 65 && num <= 90) { ch = num; } else if (num >= 97 && num <= 122) { ch = num; } else { cout << "Invalid input." << endl; return 0; } cout << "The corresponding character is: " << ch << endl; num = 97; cout << "Enter an integer: " << num << endl; if (num >= 65 && num <= 90) { ch = num; } else if (num >= 97 && num <= 122) { ch = num; } else { cout << "Invalid input." << endl; return 0; } cout << "The corresponding character is: " << ch << endl; return 0; } |
Enter an integer: 65 The corresponding character is: A Enter an integer: 97 The corresponding character is: a
Time Complexity: O(1), as there are no loops involved.
Auxiliary Space: O(1), as we are only using a single character variable to store the result.
Approach Name: Arithmetic Conversion
Steps:
- Calculate the number of digits in the input int value.
- Iterate through the digits from right to left, extracting each digit and adding the ASCII value of ‘0’ to convert it to a char.
- Store the resulting char array in the provided output buffer.
C++
#include <iostream> #include <cstring> using namespace std; void int_to_char( int num, char *result) { int temp = num; int len = 0; while (temp > 0) { len++; temp /= 10; } for ( int i = len - 1; i >= 0; i--) { result[i] = num % 10 + '0' ; num /= 10; } result[len] = '\0' ; } int main() { int num = 12345; char result[100]; int_to_char(num, result); cout << result << endl; return 0; } |
12345
Time complexity: O(log10 n), where n is the input int value.
Space complexity: O(log10 n), where n is the input int value, due to the need to store the output char array.
C++ Program For int to char Conversion
In this article, we will learn how to convert int to char in C++. For this conversion, there are 5 ways as follows:
- Using typecasting.
- Using static_cast.
- Using sprintf().
- Using to_string() and c_str().
- Using stringstream.
Let’s start by discussing each of these methods in detail.
Examples:
Input: N = 65
Output: AInput: N = 97
Output: a
Contact Us