Convert Char to String
Convert Char to String in C++ (5 Programs)
BeginnerTopic: String Conversion Programs
C++ Convert Char to String Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <string>
using namespace std;
int main() {
char ch = 'A';
// Method 1: Using string constructor
string str1(1, ch);
// Method 2: Using assignment operator
string str2 = "";
str2 = ch;
// Method 3: Using push_back
string str3 = "";
str3.push_back(ch);
cout << "Character: " << ch << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;
cout << "String (method 3): " << str3 << endl;
return 0;
}Output
Character: A String (method 1): A String (method 2): A String (method 3): A
Understanding Convert Char to String
This program demonstrates 5 different methods to convert a character to a string in C++: using string constructor, assignment operator, push_back, stringstream, and using string append method.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.