C++ Tutorials

C++ Tutorials

How to convert a Char to String C++

Input: char s[] = { 'n', 'e', 'd', 'c', 'o', 'd'} ;
Output: string s = "nedcod" ;

Input: char s[] = { 'c', 'o', 'd', 'e', 'r' } ;
Output: string s = "coder" ;
// CPP program to get a Char to String C++
using namespace std;
string convertToString(char* a, int size) 
    int i; 
    string s = ""; 
    for (i = 0; i < size; i++) { 
        s = s + a[i]; 
    return s; 

int main() 
    char a[] = { 'C', 'O', 'D', 'E', 'R' }; 
    char b[] = "nedcod"; 
    int a_size = sizeof(a) / sizeof(char); 
    int b_size = sizeof(b) / sizeof(char); 
    string s_a = convertToString(a, a_size); 
    string s_b = convertToString(b, b_size); 
    cout << s_a << endl; 
    cout << s_b << endl; 
    return 0; 

Hi, we will learn about strings in C++. C++ comes with a library for string which comes with very important and useful functions. Let’s learn about them in this tutorial.

C++ string class internally uses char array to store character but all memory management, allocation and null termination is handled by string class itself that is why it is easy to use.

The length of c++ string can be changed at runtime because of dynamic allocation We will be using the library bits/stdc++.h from here on as it includes all the libraries in C++.

To declare a string variable we write string followed by the variable name. Just like, normal variables. To initialize a string we write the string in double quotes inside parenthesis of the string. Like this.

Let’s print this string. We just need to write cout to print this string. We can see that the output is as expected. To initialize a string with multiple occurance of a character or substring, we write the number of occurrences we want then a comma followed by the substring in quotes.

Here, we are printing a string with 5 stars. Let’s run this code, we can see that the output is as expected.

We can find the length of string using .length() function. To use this, we just need to write variable name dot length() like this. To find the character at some index we use the .at function, here we are finding the value at index 4.

To clear a string, we write variable name.clear(). Let’s run this code, we can see that the output is as expected. To append a substring to a string we use .append function. Like, here in this case we are appending forgeeks to s1.

The substr function prints the substring of length b starting from index a. If we do not pass b in the function, it automatically takes this value as length-1. Let’s run this code, we can see that s2 is appended to s1. And the substring Geeks is printed. To delete a specific part of a string we use the erase function.

This function deletes a string of length b starting from the index a. Here, we are deleting a substring of length 3 from index 3. Let’s run this code, we can see that the output is as expected. To find a pattern or substring in a string we use the find function, this returns the index at which the substring is found, else it returns 0.

Like, here we are finding “for” in “GeeksforGeeks”. To replace a substring of a string, we use .replace function. This function replaces b characters from a index with “str”. Here, we are replacing for with “are”.

Let’s run this code, we can see that for is found at the index 5 and it is replaced with are. This brings us to the end of this video tutorial, thank you for watching please leave us your likes and comments in the comments section.

Char Array to String C++

char arr[ ] = "This is a test";

string str(arr);

//  You can also assign directly to a string.
str = "This is another string";

// or
str = arr;
Using STL then do this:

string firstLetter(1,str[0]);

1 Char to String C++

#include <sstream>
#include <string>
std::string s(1, c); std::cout << s << std::endl;


std::cout << std::string(1, c) << std::endl;


std::string s; s.push_back(c); std::cout << s << std::endl;

Append Char to String C++

Use += operator instead of named functions.

int main()
  char d[1] = { 'd' };
  std::string y("Hello worl");
  std::cout << y;
  return 0;

Can i Convert Char to String C++



int main()
char c = ‘A’;

// using string class fill constructor

std::string s(1, c);
std::cout << s << '\n';

return 0;


Const Char* to String c++

std::stringhas a constructor fromconst char *.This means that it is legal to write:

const char* str="hello";
std::string s = str;

Cannot Convert From Char to String C++

you change from a string * that you no longer need to dereference it in your cout:

if (cool){
    for (int i=0; i<counter; i++) cout << str << endl;
} else {
    cout << str << endl;