I am trying to copy one string to char array, string have multiple NULL character. My problem is when first NULL character encountered my program stops copying the string.
I have used two approaches. This is what I am so far.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
std::string str = "Hello World.\0 How are you?\0";
char resp[2000];
int i = 0;
memset(resp, 0, sizeof(resp));
/* Approach 1*/
while(i < str.length())
{
if(str.at(i) == '\0')
str.replace(str.begin(), str.begin()+i, " ");
resp[i] = str.at(i);
i++;
}
/* Approach 2*/
memcpy(resp, str.c_str(), 2000);
cout << resp << endl;
return 0;
}
This program should print Hello World. How are you?
. Please help me to correct this.