How to convert a single character to string in C++?

There are sure occasions in C++ programming when it is important to change over a specific information type to another; one such transformation is from a string to an int. How about we examine a couple of the ways of changing a string over to an int:

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

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

C++ upholds different string and character types, and gives ways of communicating exacting upsides of every one of these sorts. In your source code, you express the substance of your person and string literals utilizing a person set. Widespread person names and getaway characters permit you to communicate any string utilizing just the essential source character set. A crude string strict empowers you to try not to utilize get away from characters, and can be utilized to communicate a wide range of string literals. You can likewise make std::string literals without performing additional development or change steps.

Convert a single character to string in C++

Need to gain from the best arranged recordings and practice issues, look at the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for establishment in addition to STL. To finish your readiness from learning a language to DS Algo and some more, if it’s not too much trouble, allude Complete Interview Preparation Course.

1. Utilizing the stringstream class

The stringstream class is utilized to perform input/yield procedure on string-based streams. The << and >> administrators are utilized to separate information from(<<) and embed information into (>>) the stream. Investigate the model below:​

#include <iostream>
#include <sstream>
using namespace std;
int main() {
  string str = “100”; // a variable of string data type
  int num; // a variable of int data type
  // using the stringstream class to insert a string and
  // extract an int
  stringstream ss;
  ss << str;
  ss >> num;
  cout << “The string value is ” << str << endl;
  cout << “The integer representation of the string is ” << num << endl;
}

2. Utilizing stoi()

The stoi() work accepts a string as a boundary and returns the number portrayal. Investigate the model beneath:

#include <iostream>
#include<string>
using namespace std;
int main() {
  // 3 string examples to be used for conversion
  string str_example_1 = “100”;
  string str_example_2 = “2.256”;
  string str_example_3 = “200 Educative”;
  // using stoi() on various kinds of inputs
  int int_1 = stoi(str_example_1);
  int int_2 = stoi(str_example_2);
  int int_3 = stoi(str_example_3);
  cout << “int_1 : ” << int_1 << endl;
  cout << “int_2 : ” << int_2 << endl;
  cout << “int_3 : ” << int_3 << endl;
}

3. Utilizing atoi()

The atoi() work is unique in relation to the stoi() work in a couple of ways. To begin with, atoi() changes over C strings (invalid ended person clusters) to a whole number, while stoi() changes over the C++ string to a number. Second, the atoi() capacity will quietly fall flat on the off chance that the string isn’t convertible to an int, while the stoi() capacity will just toss an exemption.

Investigate the​ ​usage of atoi() beneath:

#include <iostream>
#include<string>
using namespace std;
int main() {
  // 3 string examples to be used for conversion
  const char* str_example_1 = “100”;
  const char* str_example_2 = “2.256”;
  const char* str_example_3 = “200 Educative”;
  // using stoi() on various kinds of inputs
  int int_1 = atoi(str_example_1);
  int int_2 = atoi(str_example_2);
  int int_3 = atoi(str_example_3);
  cout << “int_1 : ” << int_1 << endl;
  cout << “int_2 : ” << int_2 << endl;
  cout << “int_3 : ” << int_3 << endl;
}
// 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 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

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.

using =/+= operator

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

// 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

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);
// 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

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);
// 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

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.

// 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.

Also Read: How to delete a row in MATLAB?

Leave a Reply

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