C++ Programming Code Examples C++ > Strings Code Examples Compare two strings in C++ programming language Compare two strings in C++ programming language strcmp(string1, string2) This function will return 0, if the strings are equal; negative value, if string1 is less than string2 and positive value if string1 is greater than string2. strcmp compares strings in lexicographical (alphabetical) order. "less than" for strings means that "cat" is less than "dog" because "cat" comes alphabetically before "dog". //declare two strings char str1[100]; char str2[100]; //get user input of strings: cout << "Please enter the first string\n"; cin >> str1; cout << "Please enter the second string\n"; cin >> str2; //compare strings int result = strcmp(str1, str2); if (result == 0) { //strings are equals cout << str1 << " is equal to " << str2 << endl; } else { if (result > 0)//str1 is greater cout << str1 << " is greater than " << str2 << endl; else//str2 is greater cout << str1 << " is less than " << str2 << endl; Please enter the first string abc Please enter the second string abd abc is less than abd