/************************************ Write a program that reads in two strings from the user and tells the user whether one is a reverse of the other. ************************************/ #include #include using namespace std; bool reverses(string s1, string s2); int main() { // Get strings A and B string A, B; cout << "Enter string one: "; cin >> A; cout << "Enter string two: "; cin >> B; // Print whether A and B are reverses cout << "Strings are "; if (!reverses(A,B)) cout << "not "; cout << "reverses of one another!" << endl; return 0; } // Returns true if s1 and s2 are "reverses" of one another // functino returns bool call "predicate" bool reverses(string s1, string s2) { if (s1.length() != s2.length() ) { return false; } int index1 = 0; int index2 = s2.length()-1; while (index1 < s1.length()) { if (s1[index1] != s2[index2]) { return false; // not reverses } // move indices index1++; index2--; } // if make it here, it's a match return true; }