Write a program that given two character strings from the user:
Checks these two character strings to see if one string is the reverse of the other.
If the strings are reverse, display: “Reverse Strings Found”, otherwise display: “Reverse Strings Not Found”.
Create a function to perform this task.
1
Expert's answer
2014-12-23T00:59:37-0500
#include <iostream> #include <conio.h> // for getch(); _getch() using namespace std;
if (AreRevers(s1, s2)) { cout <<"Reverse Strings Found" << endl; } else { cout <<"Reverse Strings Not Found" << endl; } cout << "Press any key tocontinue..."; getch(); // or _getch() if this codedoesn't work } bool AreReverse(char* leftStr, char* rightStr) { int length = strlen(leftStr); if (length != strlen(rightStr)) // ifstrings lengths aren't equal they won't be reverse by definition { return false; } for (int i = 0, j = length - 1; i <length; i++, j--) { if (leftStr[i] !=rightStr[j]) // if beginning symbols of the first string aren't equal to last symbols of the second string { return false; } } return true; }
Comments
Leave a comment