how to convert char* to string in c++ code example
Example 1: c++ cast char to string
char sczName[] = {"Jakes"};
std::string strName = std::string(sczName);
Example 2: COnvert string to char * C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s = "geeksforgeeks";
int n = s.length();
char char_array[n + 1];
strcpy(char_array, s.c_str());
for (int i = 0; i < n; i++)
cout << char_array[i];
return 0;
}
Example 3: char to string c++
std::cout << std::string(1, c) << std::endl;
Example 4: why convert char* to string c++
char *cStr = "C++";
std::string Str = std::string(cStr);