Convert a char to a string in C++
This post will discuss various methods to convert a char to a string in C++.
1. Using std::string constructor
A simple solution is to use the string class fill constructor string (size_t n, char c); which fills the string with n copies of character c.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> int main() { char c = 'A'; // using string class fill constructor std::string s(1, c); std::cout << s << std::endl; return 0; } |
2. Using std::stringstream function
Another good alternative is to use a string stream to convert between strings and other numerical types. The idea is to insert the given character into a stream and then write the contents of its buffer to the std::string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> #include <sstream> int main() { char c = 'A'; // using stringstream std::string s; std::stringstream ss; ss << c; ss >> s; // or, use `s = ss.str()` std::cout << s << std::endl; return 0; } |
3. Using std::string::push_back function
Another commonly used solution is to use the push_back() function, which is overloaded for chars and appends a character to the string’s end.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { char c = 'A'; // using string::push_back std::string s; s.push_back(c); std::cout << s << std::endl; return 0; } |
4. Using std::string::operator+=
string& operator+= (char c);
The string +=operator is overloaded for chars. We can use it to append a character at the end of the string, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { char c = 'A'; // using `std::string::operator+=` std::string s; s += c; std::cout << s << std::endl; return 0; } |
5. Using std::string::operator=
string& operator= (char c);
Similar to the +=operator, the =operator is also overloaded for chars. We can use it to replace the contents of a string with a single char.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { char c = 'A'; // using `std::string::operator=` std::string s; s = c; std::cout << s << std::endl; return 0; } |
6. Using std::string::append function
string& append (size_t n, char c);
We can also use the append() function to append n copies of character c, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { char c = 'A'; // using `std::string::append` std::string s; s.append(1, c); std::cout << s << std::endl; return 0; } |
7. Using std::string::assign function
string& assign (size_t n, char c);
The assign() function replaces the current value of the string with n copies of character c. We can use it, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { char c = 'A'; // using `std::string::assign` std::string s; s.assign(1, c); std::cout << s << std::endl; return 0; } |
8. Using std::string::insert function
string& insert (size_t pos, size_t n, char c);
We can use insert() that inserts n copies of character c beginning at character pos.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { char c = 'A'; // using `std::string::insert` std::string s; s.insert(0, 1, c); std::cout << s << std::endl; return 0; } |
9. Using std::string::replace function
string& replace (size_t pos, size_t len, size_t n, char c);
The replace() function replaces the portion of the string (len characters beginning at character pos) by n copies of character c. We can use it, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { char c = 'A'; // using `std::string::replace` std::string s; s.replace(0, 1, 1, c); std::cout << s << std::endl; return 0; } |
10. Converting char to C-string
Finally, we can also convert the char to a C-string and then convert the C-string to a std::string using the fill constructor or std::string::append or std::string::assign.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <string> int main() { char c = 'A'; // converting char to C-string const char* str = &c; // using `std::string` fill constructor std::string s(str, 1); std::cout << s << std::endl; // using `std::string::append` std::string S; S.append(str, 1); std::cout << S << std::endl; // using `std::string::assign` S.assign(str, 1); std::cout << S << std::endl; return 0; } |
That’s all about converting a char to a string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)