How to Convert a Single Character to String in C++?
Last Updated :
04 Nov, 2024
A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++.
Example:
Input: c = 'A'
Output: s = "a"
Explanation: Character c is converted to a string.
Input: c = '#'
Output: s = "#"
Explanation: Character c is converted to a string.
C++ provides a lot of different methods to convert the single character to string:
Using String Constructor
We can directly convert the single character to std::string using its constructor at the time of its declaration.
Syntax
string s(1, c);
where, c is the character and 1 denotes the length of the string.
Example
C++
// C++ program to convert the single character
// to string using string constructor
#include <bits/stdc++.h>
using namespace std;
int main() {
char c = 'A';
// Create an string with one occurence of c
string s(1, c);
cout << s;
return 0;
}
Using + Operator
In std::string, the + operator is overloaded to add the given character or a string at the of a given string.
Example
C++
// C++ program to convert the single character to
// string using + operator
#include <bits/stdc++.h>
using namespace std;
int main() {
char c = 'A';
string s;
// Appending character c to the end of string
s = s + c;
cout << s;
return 0;
}
Using string::push_back() or string::append()
The string::push_back() method is used to append() a single character at the end of the string. We can use this method to convert the single character to string by pushing one character to an empty string.
The string::append() method is similar to above method, but with small syntax difference.
Example
C++
// C++ program to convert the single character
// to string using string::push_back() or
// string::append()
#include <bits/stdc++.h>
using namespace std;
int main() {
char c1 = 'A';
char c2 = '#';
string s1;
string s2;
// Appending the character to the end of string
// using string::push_back()
s1.push_back(c1);
// Appending the character to the end of string
// using string::append()
s2.append(1, c2);
cout << s1 << endl;
cout << s2;
return 0;
}
Using string::assign()
We can also convert the single character to string using string::assign() method in a similar way as previous method.
Example
C++
// C++ program to convert the single character
// to string using string::assign()
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
char c = 'A';
// Converting the single character to string
s.assign(1, c);
cout << s;
return 0;
}
Using std::stringstream
We can also use the std::stringstream method to convert the single character to string. In this method, first we create an object of type std::stringstream which can be used to output data in a string buffer. After that, we insert the character to std::stringstream object using << operator. The resultant string is then extracted using stringstream::str() member method. Though, this method is an overkill and is not recommended.
Example
C++
// C++ program to convert the single character
// to string using std::stringstream
#include <bits/stdc++.h>
using namespace std;
int main() {
char c = 'A';
// Create an std::stringstream object
stringstream ss;
// Appending the character to stream
ss << c;
// Extract the string from the stream
string s = ss.str();
cout << s;
return 0;
}
We can also convert the single character to std::string using std::format() method. In this method, we have to pass the placeholder {}, so that the character is replaced by this placaeholder.
Syntax
std::format("{}", c);
where, {} is the placeholder and c is the character to convert.
Example
C++
// C++ program to convert the single character
// to string using std::format()
#include <bits/stdc++.h>
using namespace std;
int main() {
char c = 'A';
// Converting the single character to string
// by replacing placeholder {} with character c
string s = format("{}", c);
cout << s;
return 0;
}
Output
A