How to Convert a Char to a String in C++

  1. Use string::string(size_type count, charT ch) Constructor to Convert a Char to a String
  2. Use push_back() Method to Convert a Char to a String
  3. Use the append() Method to Convert a Char to a String in C++
  4. Use the insert() Method to Convert a Char to a String in C++

This article will demonstrate multiple methods for converting a char to a string in C++.

Use string::string(size_type count, charT ch) Constructor to Convert a Char to a String

This method uses one of the std::string constructors to convert a character for a string object in C++. The constructor takes 2 arguments: a count value, which is the number of characters a new string will consist of, and a char value assigned to each character. Note that this method defines the CHAR_LENGTH variable for better readability. We can pass the integer literal directly to the constructor.

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

constexpr int CHAR_LENGTH = 1;

int main(){
    char character = 'T';

    string tmp_string(CHAR_LENGTH, character);
    cout << tmp_string << endl;

    return EXIT_SUCCESS;
}

Output:

T

Use push_back() Method to Convert a Char to a String

Alternatively, we can utilize the push_back built-in method to convert a character into a string variable. At first, we declare an empty string variable and then use the push_back() method to append a char. Based on the example, we declare the char variable named character, which is later passed as an argument to the push_back command. Still, you can directly specify the literal value as a parameter.

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(){
    char character = 'T';

    string tmp_string;
    tmp_string.push_back(character);
    cout << tmp_string << endl;

    return EXIT_SUCCESS;
}

Output:

T

Use the append() Method to Convert a Char to a String in C++

The append method is a member function of the std::string class, and it can be used to append additional characters to the string object. In this case, we would only need to declare an empty string and add a char to it as demonstrated in the following example code:

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(){
    char character = 'T';

    string tmp_string;
    tmp_string.append(1, character);
    cout << tmp_string << endl;

    return EXIT_SUCCESS;
}

Output:

T

Use the insert() Method to Convert a Char to a String in C++

The insert method is also part of the std::string class. This member function can insert a given char to a specific position in a string object specified by the first argument. The second argument denotes the number of copies of the characters to be inserted in the place.

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(){
    char character = 'T';

    string tmp_string;
    tmp_string.insert(0, 1, character);
    cout << tmp_string << endl;

    return EXIT_SUCCESS;
}

Output:

T

Related Article - C++ String

  • How to Add Int to String in C++
  • How to Tokenize a String in C++