Skip to content
Related Articles

Related Articles

Improve Article
How to convert a single character to string in C++?
  • Difficulty Level : Basic
  • Last Updated : 27 Jan, 2021

How to convert a single character to a string object.
Examples: 
 

Input :  x = 'a'
Output : string s = "a"

Input :  x = 'b'
Output : string s = "b"

 

The idea is to use string class which has a constructor that allows us to specify size of 
string as first parameter and character to be filled in given size as second parameter.

This is called Class-fill constructor.
 

// Create a string of size n and fill
// the string with character x.
string s(size_t n, char x);

NOTE- size_t is typedef of unsigned long long hence we should not pass negative parameters.



 

CPP




// CPP program to get a string from single
// character.
#include<bits/stdc++.h>
using namespace std;
 
string getString(char x)
{
    // string class has a constructor
    // that allows us to specify size of
    // string as first parameter and character
    // to be filled in given size as second
    // parameter.
    string s(1, x);
 
    return s;  
}
 
int main() {
  cout << getString('a');
  return 0;
}
Output
a

However there are many other ways by which you can convert character into string. The method mentioned above only works while initialization of string instance.
 

1- using =/+= operator

This is very Basic method and these operators are overloaded to assign or append a character.

C++




// CPP program to get a string from single
// character.
#include<bits/stdc++.h>
using namespace std;
 
 
int main() {
  char ex = 'G';
  string str;
 
  // Using += operator (here we append character to end of string)
  str += ex;
  cout << str << endl;
 
  // using = operator (overwrites the string)
  str = "GeekForGeeks";
  str = ex;
  cout << str << endl;
}
Output
G
G

2- std::string::append()

This method is just similar as += operator discussed above but It gives us another advantage. By using this method we can append as many characters we want.

// appends n copy of character x at end of string
string s(size_t n, char x);

C++




// CPP program to get a string from single
// character.
#include<bits/stdc++.h>
using namespace std;
 
 
int main() {
  string str;
  str.append(1, 'G');
  cout << str << endl;
}
Output
G

3- std::string::assign()

This method is just similar as = operator discussed above but It gives us another advantage. By using this method we can append as many characters we want.

// assigns n copy of character x to the string
string s(size_t n, char x);

C++




// CPP program to get a string from single
// character.
#include<bits/stdc++.h>
using namespace std;
 
 
int main() {
  string str = "GeekForGeeks";
  // assign method overwrites initial content
  str.assign(1, 'G');
  cout << str << endl;
}
Output
G

4- std::stringstream

This method is probably more rare in use. I personally don’t prefer this method until and unless I have wide use of it. For example you can use it if your program has multiple conversions such as string to int, floats etc.

C++




// CPP program to get a string from single
// character by stringstream.
#include<bits/stdc++.h>
using namespace std;
 
int main() {
  stringstream stream;
 
  // adding our character to stream
  char ex = 'G';
  stream << ex;
 
  // retrieving back our input into string
  string str;
  stream >> str;
 
  cout << str << endl;
}
Output
G

These were some methods where we can get char from string. However there are many more methods such as use of replace, insert method but that is unnecessary and quite rare.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :