how to convert char array to string c++ code example
Example 1: string to char array c++
std::string myWord = "myWord";
char myArray[myWord.size()+1];
strcpy(myArray, myWord.c_str());
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: c++ char to string
#include <iostream>
using namespace std;
int main()
{
char c = 'l';
string str;
str.push_back(c);
}
Example 4: c++ string to char array
const char *array = tmp.c_str();
char *array = &tmp[0];