
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to convert a single character to its integer value in Python?
- Java program to convert a character array to string
- Java Program to Convert Character to String
- Convert character array to string in Arduino
- Convert string to character array in Arduino
- How to convert list elements into a single string in R?
- How to join two strings to convert to a single string in Python?
- How do you convert a string to a character array in JavaScript?
- 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
- C# Program to Convert Character case
- How to convert a single char into an int in C++
- How to convert a string into int in C#?
Advertisements