Convert Char to String in C++
Turn a char into a std::string in C++ three ways: the string(1, ch) constructor, assignment, and push_back. Shows which forms compile and why string(ch) fails.
1#include <iostream>2#include <string>3using namespace std;45int main() {6 char ch = 'A';78 // Method 1: Using string constructor9 string str1(1, ch);1011 // Method 2: Using assignment operator12 string str2 = "";13 str2 = ch;1415 // Method 3: Using push_back16 string str3 = "";17 str3.push_back(ch);1819 cout << "Character: " << ch << endl;20 cout << "String (method 1): " << str1 << endl;21 cout << "String (method 2): " << str2 << endl;22 cout << "String (method 3): " << str3 << endl;2324 return 0;25}
Sample Output
String (method 1): A
Built by the constructor.
String (method 3): A
Built by push_back.
In this one
- How the string(count, char) constructor works
- That assigning a char to an existing string is allowed
- How push_back appends one character
- Why string s(ch) with one char argument does not compile
When to use this
Use this when an API wants a string but you only have one character, for example building a word letter by letter or joining a char to other text.
How it works
- 1
Store 'A' in ch.
- 2
str1(1, ch) builds a string of one copy of ch.
- 3
str2 = ch replaces str2's contents with that one character.
- 4
str3.push_back(ch) appends it to an empty string.
- 5
Print all four values.
Edge cases
Constructor with one argument
Write string s(ch);
There is no string constructor that takes a single char, so this fails to compile. The count argument in string(1, ch) is required.
Initializing with = ch
Write string s = ch; in one line.
This also fails to compile, because copy-initialization from a char has no matching constructor. Assignment to an existing string (str2 = ch) is a different operator and works.
What's going on
The program converts a character (like 'A') into a string (like "A"). While a character is a single letter or symbol, a string is a sequence of characters. This conversion is necessary when you want to use string methods or combine characters with other strings.
Example:
Header Files Used
Declaring Variables
The program declares:
char ch = 'A';
Method 1: Using String Constructor
string str1(1, ch);
This method uses the string constructor that takes two parameters:
So string(1, 'A') creates a string containing one 'A', which becomes "A".
This is the most direct and efficient method for converting a single character to a string.
Method 2: Using Assignment Operator
string str2 = "";
str2 = ch;
This method works in two steps:
When you assign a character to a string, C++ automatically converts it to a string. This is simple but requires two lines of code.
Method 3: Using push_back()
string str3 = "";
str3.push_back(ch);
This method uses the push_back() function:
push_back() is a string method that adds a single character to the end of a string. It's useful when you're building a string character by character.
Other Methods (Mentioned but not shown in code)
The program description mentions two additional methods:
Method 4: Using stringstream
#include <sstream>
stringstream ss;
ss << ch;
string str4 = ss.str();
This uses stringstream to convert the character. It's useful when you need to convert multiple values.
Method 5: Using string append method
string str5 = "";
str5.append(1, ch);
The append() method is similar to push_back() but can add multiple characters at once.
Displaying Results
The program prints all three converted strings:
cout << "Character: " << ch << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;
cout << "String (method 3): " << str3 << endl;
All three methods produce the same result: "A"
When to Use Each Method
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does string s(3, 'x') hold?
Pick an answer to check it.
Question 2
Which line safely assigns ch to a string?
Pick an answer to check it.
Questions people ask
Which method should I use?
string(1, ch) when creating a new string, push_back or += when adding to an existing one.
Can I do "" + ch?
No. "" is a char pointer, and pointer + char does pointer arithmetic. Use string("") + ch instead.
What about string s{ch}?
Brace initialization picks the initializer-list constructor, so it creates a one-character string.