Convert Char to String in C++

Convert Char to String in C++ (5 Programs).

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}

Output

Character: A
String (method 1): A
String (method 2): A
String (method 3): A

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

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.

1#include <sstream>

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:

All three methods produce the same result: "A"

1cout << "Character: " << ch << endl;
2cout << "String (method 1): " << str1 << endl;
3cout << "String (method 2): " << str2 << endl;
4cout << "String (method 3): " << str3 << endl;

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.