Game Development Reference
In-Depth Information
Working with C Style Strings
C++ provides functions to work with C style strings. We cover how functions operate and write our
own in Chapter 5; for now we list the different functions that C++ provides to work with strings.
strlen
strlen tells us how many characters are in a given string excluding the null terminator; this is
shown in Listing 4-7.
Listing 4-7. strlen
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char* cStyleString = "CStyleString";
cout << strlen(cStyleString) << endl;
return 0;
}
This program outputs 12.
strcmp
strcmp compares two strings and returns 0 if they match. Listing 4-8 contains an example.
Listing 4-8. strcmp
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char* cStyleString1 = "CStyleString";
char* cStyleString2 = "CStyleString";
cout << strcmp(cStyleString1, cStyleString1) << endl;
return 0;
}
This program outputs 0.
 
Search WWH ::




Custom Search