
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to convert a single character to string in C++?
There are several methods to convert a single character to a string. In the following example, some of them are used to convert a character to a string.
Here is an example of converting a single character to string in C++ language,
Example
#include <iostream> #include<string> #include<sstream> int main() { char c = 'm'; std::string s(1, c); std::cout << "Using string constructor : " << s << '\n'; std::string s2; std::stringstream s1; s1 << c; s1 >> s; std::cout << "Using string stream : " << s << '\n'; s2.push_back(c); std::cout << "Using string push_back : " << s2 << std::endl; return 0; }
Output
Here is the output
Using string constructor : m Using string stream : m Using string push_back : m
In the above program, three methods are used to convert a character to a string. First, using string constructor
std::string s(1, c);
Second, using string stream
std::string s2; std::stringstream s1; s1 << c; s1 >> s;
Third, using string push_back
s2.push_back(c);
- Related Articles
- How to convert a single character to its integer value in Python?
- How to join two strings to convert to a single string in Python?
- How to convert list elements into a single string in R?
- How do you convert a string to a character array in JavaScript?
- Convert string to character array in Arduino
- Convert character array to string in Arduino
- Java program to convert a character array to string
- Java Program to Convert Character to String
- Haskell Program to Convert Character to String
- Golang Program to Convert Character to String
- Append a single character to a string or char array in java?
- C# program to convert several strings into a single comma-delimited string
- How to convert a list to string in C#?
- How to convert a string to a integer in C
- Different methods to append a single character to a string or char array in Java

Advertisements