![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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);
Advertisements