Convert Char to String in C++
Convert Char to String in C++ (5 Programs).
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}
Output
Character: A String (method 1): A String (method 2): A String (method 3): A
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
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.
1#include <sstream>
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:
All three methods produce the same result: "A"
1cout << "Character: " << ch << endl;2cout << "String (method 1): " << str1 << endl;3cout << "String (method 2): " << str2 << endl;4cout << "String (method 3): " << str3 << endl;
When to Use Each Method
-
String Constructor (Method 1)
: Best for simple, one-time conversions. Most efficient.
-
Assignment Operator (Method 2)
: Good for simple cases, easy to read.
-
push_back() (Method 3)
: Useful when building strings character by character in loops.
-
stringstream
: Best when converting multiple values or mixing types.
-
append()
: Useful when you need to add multiple characters.
return 0;
This ends the program successfully.