/* teststr1.cpp CIS 150 6/7/2005 David Klick Demonstration of C-style string functions. */ #include #include using std::cout; using std::boolalpha; int main(void) { // create various strings char str1[] = "My string"; char str2[] = "My string"; char str3[] = "my string"; char str4[] = "Your string"; // display strings to user cout << "str1: " << str1 << '\n'; cout << "str2: " << str2 << '\n'; cout << "str3: " << str3 << '\n'; cout << "str4: " << str4 << '\n'; // demonstrate use of equality operator cout << boolalpha; cout << "str1 == str2 is " << (str1==str2) << '\n'; cout << "str1 == str3 is " << (str1==str3) << '\n'; cout << "str1 == str4 is " << (str1==str4) << '\n'; // now use strcmp(char*, char*) function // (case sensitive comparison) cout << "For strcmp(a, b) and stricmp(a, b):\n" << " a result < 0 means a < b\n" << " a result > 0 means a > b\n" << " a result == 0 means a == b\n"; cout << "strcmp(str1, str2) is " << strcmp(str1, str2) << '\n'; cout << "strcmp(str1, str3) is " << strcmp(str1, str3) << '\n'; cout << "strcmp(str1, str4) is " << strcmp(str1, str4) << '\n'; // now use stricmp(char*, char*) function // (case insensitive comparison) cout << "stricmp(str1, str2) is " << stricmp(str1, str2) << '\n'; cout << "stricmp(str1, str3) is " << stricmp(str1, str3) << '\n'; cout << "stricmp(str1, str4) is " << stricmp(str1, str4) << '\n'; // demonstrate strlen(char*) function cout << "The length of str1 is " << strlen(str1) << '\n'; cout << "The length of str2 is " << strlen(str2) << '\n'; cout << "The length of str3 is " << strlen(str3) << '\n'; cout << "The length of str4 is " << strlen(str4) << '\n'; return 0; } /* Sample output: str1: My string str2: My string str3: my string str4: Your string str1 == str2 is false str1 == str3 is false str1 == str4 is false For strcmp(a, b) and stricmp(a, b): a result < 0 means a < b a result > 0 means a > b a result == 0 means a == b strcmp(str1, str2) is 0 strcmp(str1, str3) is -1 strcmp(str1, str4) is -1 stricmp(str1, str2) is 0 stricmp(str1, str3) is 0 stricmp(str1, str4) is -1 The length of str1 is 9 The length of str2 is 9 The length of str3 is 9 The length of str4 is 11 */