Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a single character to string in C++?
In C++, a single character is represented using a single quotation (' ') while a string is represented using a double quotation (" "). To change the single character into a string, use approaches like string_constructor, push_back(), etc., that solve the conversion.
Example
Input: ch = 'A' Output: str = A // "A"
So, character A is converted to a string.
C++ provides various approaches to convert single character into string as follows:
Using string constructor
The string constructor is a very simple way to create a string from the single character.
Example
Here, we pass the character T to the string constructor to convert one character string, and it displays the result.
#include <iostream>
#include <string>
using namespace std;
int main() {
char ch = 'T';
string str(1, ch); // "T"
cout << "The result string is " << str;
return 0;
}
Output
The above program produces the following result:
The result string is T
Using push_back()
The push_back() is a C++ STL function that allows the user to add/push the element. Here, we have initialized the string input and then used the same variable for the string object name to push the character to make it a string.
Example
In this example, we added character Z to an empty string using pushback() and a function associated with the string variable to change the character into a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
char ch = 'Z';
string str = "";
str.push_back(ch); // "Z"
cout << "The string result is " << str;
return 0;
}
Output
The above program produces the following result:
The string result is Z
Using stringstream
The stringstream class belongs to the <sstream> header that is used to change the conversion of character into string.
Example
Here, a character 'P' is inserted into the stringstream object, i.e., ss, that helps to change the character into a string result.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
char ch = 'P';
stringstream ss;
ss << ch;
string str = ss.str(); // "P"
cout << "The string result is " << str;
return 0;
}
Output
The above program produces the following result:
The string result is P
Using += operator
To perform the character-to-string conversion, use the += operator to get the desired result.
Example
In this example, we create an empty string, and the character 'K' is appended using the += operator, which changes the character into a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
char ch = 'k';
string str = "";
str += ch; // "k"
cout << "The string result is " << str;
return 0;
}
Output
The above program produces the following result:
The string result is k