Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Convert Char to String in C++

Turn a char into a std::string in C++ three ways: the string(1, ch) constructor, assignment, and push_back. Shows which forms compile and why string(ch) fails.

BeginnerString Conversion ProgramsExample 1 of 11
convert-char-to-string.cpp
1#include <iostream>
2#include <string>
3using namespace std;
4
5int main() {
6 char ch = 'A';
7
8 // Method 1: Using string constructor
9 string str1(1, ch);
10
11 // Method 2: Using assignment operator
12 string str2 = "";
13 str2 = ch;
14
15 // Method 3: Using push_back
16 string str3 = "";
17 str3.push_back(ch);
18
19 cout << "Character: " << ch << endl;
20 cout << "String (method 1): " << str1 << endl;
21 cout << "String (method 2): " << str2 << endl;
22 cout << "String (method 3): " << str3 << endl;
23
24 return 0;
25}

Sample Output

String (method 1): A

Built by the constructor.

String (method 3): A

Built by push_back.

In this one

  • How the string(count, char) constructor works
  • That assigning a char to an existing string is allowed
  • How push_back appends one character
  • Why string s(ch) with one char argument does not compile

When to use this

Use this when an API wants a string but you only have one character, for example building a word letter by letter or joining a char to other text.

How it works

  1. 1

    Store 'A' in ch.

  2. 2

    str1(1, ch) builds a string of one copy of ch.

  3. 3

    str2 = ch replaces str2's contents with that one character.

  4. 4

    str3.push_back(ch) appends it to an empty string.

  5. 5

    Print all four values.

Edge cases

Constructor with one argument

Write string s(ch);

There is no string constructor that takes a single char, so this fails to compile. The count argument in string(1, ch) is required.

Initializing with = ch

Write string s = ch; in one line.

This also fails to compile, because copy-initialization from a char has no matching constructor. Assignment to an existing string (str2 = ch) is a different operator and works.

What's going on

The program converts a character (like 'A') into a string (like "A"). While a character is a single letter or symbol, a string is a sequence of characters. This conversion is necessary when you want to use string methods or combine characters with other strings.

Example:

Character: 'A' (single quote)
String: "A" (double quotes)

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <string>
Essential for using the string data type and its methods.
Without this header, you cannot use string variables or string functions.

Declaring Variables

The program declares:

char ch = 'A';

char is the data type for storing a single character.
ch is the variable name storing the character 'A'.
Single quotes (' ') are used for characters in C++.

Method 1: Using String Constructor

string str1(1, ch);

This method uses the string constructor that takes two parameters:

First parameter (1): The number of times to repeat the character.
Second parameter (ch): The character to repeat.

So string(1, 'A') creates a string containing one 'A', which becomes "A".

This is the most direct and efficient method for converting a single character to a string.

Method 2: Using Assignment Operator

string str2 = "";

str2 = ch;

This method works in two steps:

First, create an empty string: str2 = ""
Then, assign the character directly: str2 = ch

When you assign a character to a string, C++ automatically converts it to a string. This is simple but requires two lines of code.

Method 3: Using push_back()

string str3 = "";

str3.push_back(ch);

This method uses the push_back() function:

First, create an empty string: str3 = ""
Then, use push_back(ch) to add the character to the end of the string.

push_back() is a string method that adds a single character to the end of a string. It's useful when you're building a string character by character.

Other Methods (Mentioned but not shown in code)

The program description mentions two additional methods:

Method 4: Using stringstream

#include <sstream>

stringstream ss;

ss << ch;

string str4 = ss.str();

This uses stringstream to convert the character. It's useful when you need to convert multiple values.

Method 5: Using string append method

string str5 = "";

str5.append(1, ch);

The append() method is similar to push_back() but can add multiple characters at once.

Displaying Results

The program prints all three converted strings:

cout << "Character: " << ch << endl;

cout << "String (method 1): " << str1 << endl;

cout << "String (method 2): " << str2 << endl;

cout << "String (method 3): " << str3 << endl;

All three methods produce the same result: "A"

When to Use Each Method

String Constructor (Method 1): Best for simple, one-time conversions. Most efficient.
Assignment Operator (Method 2): Good for simple cases, easy to read.
push_back() (Method 3): Useful when building strings character by character in loops.
stringstream: Best when converting multiple values or mixing types.
append(): Useful when you need to add multiple characters.

return 0;

This ends the program successfully.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does string s(3, 'x') hold?

Pick an answer to check it.

Question 2

Which line safely assigns ch to a string?

Pick an answer to check it.

Questions people ask

Which method should I use?

string(1, ch) when creating a new string, push_back or += when adding to an existing one.

Can I do "" + ch?

No. "" is a char pointer, and pointer + char does pointer arithmetic. Use string("") + ch instead.

What about string s{ch}?

Brace initialization picks the initializer-list constructor, so it creates a one-character string.