Game Development Reference
In-Depth Information
strcpy
strcpy
copies a string from one array into another. Listing 4-8 provides an example.
Listing 4-8.
strcpy
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char* cStyleString1 = "CStyleString";
char cStyleString2[13];
strcpy(cStyleString2, cStyleString1);
cout << cStyleString2 << endl;
return 0;
}
It's important to note that
cStyleString2
contains enough space to store the 12 characters of the
string literal plus an extra character for the null terminator.
strcat
strcat
allows us to join, or concatenate, two strings. Listing 4-9 provides an example.
Listing 4-9.
strcat
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char cStyleString1[13];
strcpy(cStyleString1, "CStyle");
char* cStyleString2 = "String";
strcat(cStyleString1, cStyleString2);
cout << cStyleString1 << endl;
return 0;
}
cStyleString1
must have enough space to store the combined string, therefore we create an array
with space for 13 characters. We then use
strcpy
to copy
"CStyle"
into this array.
strcat
is used to
copy
"String"
into the end of
cStyleString1
from
cStyleString2
.