Convert a char to string in C++

In this article, we will discuss different ways to convert a char value to a string in C++.

Table Of Contents

Method 1: Using string constructor

In C++, the string class provides a constructor to intialize the string object with repeated occurrence of a character. We can pass the occurrence count as 1, and a character as argument in it. It will create a string object from that character only. This way we can convert a char to a string. Let’s see an example,

#include <iostream>
#include <string>

int main()
{
    char ch = 'V';

    // Create string object from a character
    std::string strObj(1, ch);

    std::cout << strObj << std::endl;

    return 0;
}

Output:

V

It converted a char to a string object in C++.

Advertisements

Method 2: Using string::push_back() function

Create an empty string object. Then call the string::push_back() function. It accepts a character as argument, and appends that single character to the string. Let’s see an example,

#include <iostream>
#include <string>

int main()
{
    char ch = 'V';

    // Create empty string object
    std::string strObj;

    // Add character to the empty string
    strObj.push_back(ch);

    std::cout << strObj << std::endl;

    return 0;
}

Output:

V

It converted a char to a string object in C++.

Method 3: Using std::stringstream

Create a stringstream object, and push the given character to it. Then fetch the complete string from stringstream. It will have the added character. This way we can convert a char to a string. Let’s see an example,

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    char ch = 'V';

    // Create empty string object
    std::string strObj;

    // Create a string stream
    std::stringstream streamObj;

    // add character to the stream
    streamObj << ch;

    // fetch string from the stream
    streamObj >> strObj;  

    std::cout << strObj << std::endl;

    return 0;
}

Output:

V

It converted a char to a string object in C++.

Method 4: Using += operator

Create an empty string object. Then add the given character to it using += operator of string. This way we can convert a char to a string. Let’s see an example,

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    char ch = 'V';

    // Create empty string object
    std::string strObj;

    // Add character to the empty string
    strObj += ch;

    std::cout << strObj << std::endl;

    return 0;
}

Output:

V

It converted a char to a string object in C++.

Method 5: Using assignment operator

Create an empty string object. Then assign the given character to it using = operator of string. This way we can convert a char to a string. Let’s see an example,

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    char ch = 'V';

    // Create empty string object
    std::string strObj;

    // Assign character to the string
    strObj = ch;

    std::cout << strObj << std::endl;

    return 0;
}

Output:

V

It converted a char to a string object in C++.

Method 6: Using string::append() function

Create an empty string object. Then append the given character to it using append() function of string. This way we can convert a char to a string. Let’s see an example,

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    char ch = 'V';

    // Create empty string object
    std::string strObj;

    // Append a character to the empty string
    strObj.append(1, ch);

    std::cout << strObj << std::endl;

    return 0;
}

Output:

V

It converted a char to a string object in C++.

Method 7: Using string::assign() function

Create an empty string object. Then assign the given character to it using assign() function of string. This way we can convert a char to a string. Let’s see an example,

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    char ch = 'V';

    // Create empty string object
    std::string strObj;

    // Assign a character to the string
    strObj.assign(1, ch);

    std::cout << strObj << std::endl;

    return 0;
}

Output:

V

It converted a char to a string object in C++.

Method 8: Using string::insert() function

Create an empty string object. Then insert the given character at first position in the string using insert() function of string. This way we can convert a char to a string. Let’s see an example,

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    char ch = 'V';

    // Create empty string object
    std::string strObj;

    // Insert a character at the starting of string
    strObj.insert(0, 1, ch);

    std::cout << strObj << std::endl;

    return 0;
}

Output:

V

It converted a char to a string object in C++.

Method 9: Using string::replace() function

Create an empty string object. Then replace the given character with the character at first position in the string using replace() function of string. This way we can convert a char to a string. Let’s see an example,

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    char ch = 'V';

    // Create empty string object
    std::string strObj;

    // replace first character of the string
    strObj.replace(0, 1, 1, ch);

    std::cout << strObj << std::endl;

    return 0;
}

Output:

V

It converted a char to a string object in C++.

Summary

Today we learned about several ways to convert a char to a string in C++.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top