0
0
CppHow-ToBeginner · 2 min read

C++ How to Convert Char to String Easily

In C++, you can convert a char to a std::string by using the constructor like std::string s(1, c); where c is your char, or simply by assigning it to a string with std::string s = std::string(1, c);.
📋

Examples

Input'a'
Output"a"
Input'Z'
Output"Z"
Input'\n'
Output"\n"
🧠

How to Think About It

To convert a single character to a string, think of creating a string that holds exactly one character. You can do this by telling the string to hold one copy of that character, or by appending the character to an empty string.
📐

Algorithm

1
Take the input character.
2
Create a new string that contains exactly one copy of this character.
3
Return or use the new string.
💻

Code

cpp
#include <iostream>
#include <string>

int main() {
    char c = 'x';
    std::string s(1, c); // create string with one char
    std::cout << s << std::endl;
    return 0;
}
Output
x
🔍

Dry Run

Let's trace converting char 'x' to string using std::string(1, c).

1

Initialize char

c = 'x'

2

Create string from char

s = std::string(1, c) -> s = "x"

3

Print string

Output: x

StepVariableValue
1c'x'
2s"x"
3outputx
💡

Why This Works

Step 1: Using std::string constructor

The constructor std::string(size_t n, char c) creates a string with n copies of the character c. Here, n is 1, so the string holds exactly one character.

Step 2: Assigning char to string

By creating a string this way, you convert the single char into a std::string object that can be used like any other string.

🔄

Alternative Approaches

Using string concatenation with empty string
cpp
#include <iostream>
#include <string>

int main() {
    char c = 'x';
    std::string s = "" + c; // This is incorrect, won't compile
    std::cout << s << std::endl;
    return 0;
}
This approach is incorrect because adding char to string literal pointer causes error; you must convert char to string first.
Using string concatenation with std::string
cpp
#include <iostream>
#include <string>

int main() {
    char c = 'x';
    std::string s = std::string("") + c; // append char to empty string
    std::cout << s << std::endl;
    return 0;
}
This works by starting with an empty string and adding the char, creating a string with one character.
Using std::stringstream
cpp
#include <iostream>
#include <sstream>

int main() {
    char c = 'x';
    std::stringstream ss;
    ss << c;
    std::string s = ss.str();
    std::cout << s << std::endl;
    return 0;
}
This method uses a string stream to convert char to string, but is more verbose and less efficient.

Complexity: O(1) time, O(1) space

Time Complexity

Creating a string with one character takes constant time because it only copies one character.

Space Complexity

The space used is constant, just enough to store one character plus string overhead.

Which Approach is Fastest?

Using the std::string(size_t, char) constructor is fastest and simplest compared to stringstream or concatenation.

ApproachTimeSpaceBest For
std::string(1, c)O(1)O(1)Simple and efficient conversion
std::string("") + cO(1)O(1)Readable concatenation method
std::stringstreamO(1)O(1)Flexible but verbose
💡
Use std::string s(1, c); to quickly convert a char to a string in C++.
⚠️
Trying to add a char directly to a string literal without converting causes compilation errors.