// // Program: strings_equal // // Purpose: test function for determining whether two text // strings are the same. // // Input: // Two text strings. // Output: // The length of the string, not including the ending // null character. // // (This function is similar to library function strcmp().) // #include #include // has EXIT_SUCCESS #include // has setw() // Function to determine whether two text strings are equal. // Pre: s1, s2 contain null-terminated text strings. // Post: "true" is returned if s1 and s2 are the same; // "false" is returned otherwise. bool strings_equal(char s1[], char s2[]) { int i = 0; // Loop through both strings, comparing corresponding // characters, until we either come to the end of one or // both, or we find a mismatch. while ((s1[i] != '\0') && (s2[i] != '\0') && (s1[i] == s2[i])) ++i; // If s1[i] and s2[i] are both '\0', the strings match. return ((s1[i] == '\0') && (s2[i] == '\0')); } // Main program. int main(void) { const int BUFFSIZE = 1000; char string1[BUFFSIZE]; char string2[BUFFSIZE]; cout << "Enter a text string: "; cin >> setw(BUFFSIZE) >> string1; cout << "Enter another text string: "; cin >> setw(BUFFSIZE) >> string2; if (strings_equal(string1, string2)) cout << string1 << " and " << string2 << " are the same\n"; else cout << string1 << " and " << string2 << " are not the same\n"; return EXIT_SUCCESS; }