#include #include using namespace std; //delcare function prototypes void getSentence(string&); bool isPal(string); void getStrings(string&, string&); int searchReplace(string&, string, string); void printInfo(int, string, string, string); int main() { //define variables char whatDo; string input1, search, replace; int times; //loop until exit while(whatDo != 'e') { //ask user what they would like to do cout << "Would you like to check if a (p)alindrome, (s)earch/replace, or (e)xit?: "; cin >> whatDo; //determine what to do based on user input if(whatDo == 'p') { //get the sentence wanted getSentence(input1); //determine if it was a palindrome and print if it is if(isPal(input1) == true) { cout << "You entered a palindrome!" << endl; } else { cout << "The string you entered is not a palindrome :( " << endl; } } else if(whatDo == 's') { //get the initial string getSentence(input1); //get the strings the user wants to use getStrings(search, replace); //search and replace the stuff times = searchReplace(input1, search, replace); //print out the info about the funtion printInfo(times, input1, search, replace); } else if(whatDo == 'e') { return 0; } else { cout << "Bad input, enter new request" << endl; } } } //get the sentence the user would like to use //preconditions: user has selected to either check a palindrome or search and replace //postconditions: the program has a string to use //return:nothing void getSentence(string &usIn) { //ask the user for the sentence they want to search in cin.ignore(); cout << "Enter the sentence: "; getline(cin, usIn); } //check to see if the word entered is a palindrom //preconditions: user has entered a string to be checked //postconditions: the program has checked if the string is a palindrom and returns accordingly //return: true or false for whether string is a palindrome bool isPal(string input) { //check the parts of the string starting at both ends and moving inwards for(int j=0; j < (input.size()/2); j++) { if(input[j] != input[input.size()-(j+1)]) { return false; } } //if it makes it through that then it is a palindrome return true; } //input the word we want to search for and replace //preconditions: user has selected to search and replace and has entered a sentence //postconditions: the program has the words to search and replace //reutn: nothing void getStrings(string &sea, string &rep) { cout << "Enter the word you want to search for: "; cin >> sea; cout << "Enter the word you want to replace it with: "; cin >> rep; } //replace the word with the other one //preconditions: user has inputed a sentence, and words to search and replace //postcondition: words have been replaced in the string provided //return: the number of times the work was replaced int searchReplace(string &usIn, string searchWord, string repWord) { //initialize local variables int count = 0; //while there is an instance of the word in the string, do some stuff while(usIn.find(searchWord) != -1) { //replace searchWord.size # of characters starting at usIn.find() with repword usIn.replace(usIn.find(searchWord), searchWord.size(), repWord); //increment how many times word is replaced count ++; } //print number of times word repace return count; } //prints out the info on the search and replace //preconditions: the program has replaced the words in the sentence //postconditions: none //return: nothing void printInfo(int numTimes, string sentence, string search, string replace) { //print out the info about the new sentence cout << "\nThe new string is \"" << sentence << "\"" << endl; cout << "and you replaced the word \"" << search << "\" with the word \""; cout << replace << "\" " << numTimes << " times." << endl; }