Convert character array to string in C++

This article shows how to convert a character array to a string in C++.

The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string.

Examples:



Input: char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
                     'r', 'g', 'e', 'e', 'k', 's' } ;
Output: string s = "geeksforgeeks" ;

Input: char s[] = { 'c', 'o', 'd', 'i', 'n', 'g' } ;
Output: string s = "coding" ;