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

Are you looking to convert a Char to String in C++? In this article, I will show you how you can quickly cast a char to a string in C++ without any errors or issues.

In C++, an array of character pointers can be used to represent a string. This method was used to represent string variables when the actual STL for String was not defined or up in the latest C++ Version.

char* prevString = "This method was previously used before STL.";

Now the above code was previously referred to as a string and is usually called an array of character pointers that is holding the above characters.

Convert a Char to String or Char* to String Using STL std::string Constructor

The best and quickest method to cast the char to string is using the std::string class object. Strings of single-byte characters are supported by the standard string class, which has an interface similar to that of a regular container of bytes but with additional capabilities designed to meet the needs.

Let us see in the below code example in C++ to convert Char to String using the Constructor available.

// C++ implementation of the approach
#include<bits/stdc++.h>
 
// Driver code to Convert Char to String in C++
int main()
{
    //Initializing Simple Char and Char*
    char givenChar = 'C';
    
    std::string newStringObj(1, givenChar);

    std::cout<<newStringObj<<std::endl;
     
    return 0;
}

Output:

C

Similarly, if you are having an array of character pointers holding the string then you can use the above C++ String Constructor to convert from Char. Shown in the below Code.

// C++ implementation of the approach
#include<bits/stdc++.h>
 
// Driver code to Convert Char to String in C++
int main()
{
    //Initializing Simple Char and Char*
    char givenChar = 'C';
    char* prevString = "This String is Converted from Character pointer to String";

    int size = sizeof(prevString)/sizeof(*prevString);
    
    std::string newStringObj(prevString);

    std::cout<<newStringObj<<std::endl;
     
    return 0;
}

Output:

This String is Converted from Character pointer to String

Using push_back() Method of String Class

You can easily convert the Char to a string by pushing the element present in the Char to a new String variable using the push_back() method. In String class push_back() adds the new character element to the string.

Let us see the example of usage of the push_back method to convert char to string in C++.

// C++ implementation of the approach
#include<bits/stdc++.h>
 
// Driver code to Convert Char to String in C++
int main()
{
    //Initializing Simple Char
    char givenChar = 'Z';

    //Initializing Empty String Object
    std::string newStringObj;

    newStringObj.push_back(givenChar);

    std::cout<<newStringObj;
     
    return 0;
}

Output:

Z

As already iterated above and as displayed in the above code, you can easily use the push_back method to add the char to the string object.

Making Use of the Operator+ Overload Function present for String Class

In string class, we have an operator overloaded whenever “+” is used with the string object, and hence we can simply use this operator to add the char to the back or front of the string as you want it to be added.

Let us see in the below example code and how you can quickly achieve this in the real world.

// C++ implementation of the approach
#include<bits/stdc++.h>
 
// Driver code to Convert Char to String in C++
int main()
{
    //Initializing Simple Char
    char givenChar = 'Y';

    //Initializing Empty String Object
    std::string newStringObj;

    newStringObj =+ givenChar;

    std::cout<<newStringObj;
     
    return 0;
}

Output:

Y

A newly generated string object with its value being the concatenation of the characters in LHS followed by the characters in RHS is returned by this function.

Making Use of std::string::operator= To Convet Char to String

You can use the operator= method that is defined in the string class. Using this you can easily cast the char to string in C++. Let us see the same in the below example code.

// C++ implementation of the approach
#include<bits/stdc++.h>
 
// Driver code to Convert Char to String in C++
int main()
{
    //Initializing Simple Char
    char givenChar = 'T';

    //Initializing Empty String Object
    std::string newStringObj;

    newStringObj = givenChar;

    std::cout<<newStringObj;
     
    return 0;
}

Output:

T

As shown in the above code you can directly use the equal to operator to assign the new string object to the already present character variable.

Making Use of std::string::append Method to Convert Char to String

This method is similar to the push_back method. You can use the append method to add the character to the end of the string object. Let us see that in below example code.

// C++ implementation of the approach
#include<bits/stdc++.h>
 
// Driver code to Convert Char to String in C++
int main()
{
    //Initializing Simple Char
    char givenChar = 'T';

    //Initializing Empty String Object
    std::string newStringObj = "D";

    newStringObj.append(1, givenChar);

    std::cout<<newStringObj;
     
    return 0;
}

Output:

DT

Making Use of {} Braces While Initializing String to Convert Char to String

You can use the character variable inside the curly braces during string object initialization and the new string object will have converted char value. Let us see in the below example code.

Note that this is the most efficient solution you should use if you are not able to get any of the above methods to work. Also, you should not use the sstream method as it will bloat your project with an extra library.

// C++ implementation of the approach
#include<bits/stdc++.h>
 
// Driver code to Convert Char to String in C++
int main()
{
    //Initializing Simple Char
    char givenChar = 'K';

    //Initializing Empty String Object
    std::string newStringObj{givenChar};

    std::cout<<newStringObj;
     
    return 0;
}

Output:

K
How To Convert a Char to String in C++ Quickly

Wrap Up

Hope I was helpful in providing you with efficient information on converting a char to string in C++. If you have any method that is better than any of the above-mentioned methods then please let me know in the comment section I will be happy to add it.

If you liked our answer then please follow us on FacebookTwitter and Subscribe to our Newsletter to join a great community of developers around the world. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Parse (split) or Tokenize a String in C++ Using Delimiter
  2. How to Remove Last Element From List in Python

Leave a Reply

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