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
Similar Reads
How to Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor
1 min read
Convert character array to string in C++ This article shows how to convert a character array to a string in C++. The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string. Examples: Input: ch
4 min read
Convert Vector of Characters to String in C++ In this article, we will learn different methods to convert the vector of character to string in C++.The most efficient method to convert the vector of characters to string is by using string's range constructor. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std;
2 min read
How to Convert Char Array to Int in C++ In C++, a character array is treated as a sequence of characters also known as a string. Converting a character array into an integer is a common task that can be performed using various methods and in this article, we will learn how to convert char array to int in C++. Example Input:char arr1[] ="1
2 min read
Convert char* to std::string in C++ Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char*
3 min read
Convert String to Char Array in C++ In C++, we usually represent text data using the std::string object. But in some cases, we may need to convert a std::string to a character array, the traditional C-style strings. In this article, we will learn how to convert the string to char array in C++.ExamplesInput: str = "geeksforgeeks"Output
4 min read