/*************************************************************************** Program: A2Q3.cpp Purpose: This program will tests the three functions sort(), strcmp(), palindrome(). ****************************************************************************/ #include using namespace std; // Function Prototypes char* getStr(); void sort(char *s, char *d); int strcmp(char *s1, char *s2); bool palindrome(char *s); int main(){ char *string1, *string2; int choice, comp; bool ispal; string1 = new char; string2 = new char; // menu - loop while user option != 4 do{ cout << "\nWhat would you like to do?\n" << "1. Sort a string in increasing order.\n" << "2. Compare two strings.\n" << "3. Check if a word is a palindrome.\n" << "4. Exit program.\n\n"; cin >> choice; // get user option switch(choice){ case 1: // sort a given string string1 = getStr(); // get string from user sort(string1, string2); //call sort function break; case 2: // compare two given strings string1 = getStr(); // get 1st string string2 = getStr(); // get 2nd string comp = strcmp(string1,string2); // call strcmp if (comp == 1) // string 1 > string 2 cout << "\nString 1 is GREATER THAN String 2\n"; else if (comp == 0) // string 1 == string 2 cout << "\nString 1 is EQUALS String 2\n"; else // string 1 < string 2 cout << "\nString 1 is LESS THAN String 2\n"; break; case 3: // check if a given word is a palindrome string1 = getStr(); // get string ispal = palindrome(string1); // call palindrome if (ispal == true) // is a palindrome cout << "\nThis is a palindrome\n"; else // is not a palindrome cout << "\nThis is NOT a palindrome\n"; break; default: // exit switch if (choice != 4) // default - invalid option cout << "That is not a valid choice. Please choose again.\n\n"; } }while(choice!=4); // exit program return 0; } /**************************************************************************** Function: getStr Purpose: This function will prompt the user to enter a string Parameters: NONE Return Value: char pointer to string ****************************************************************************/ char* getStr(){ char *str; str = new char; if(str == NULL){ cout << "\nERROR - Insufficient memory\n"; exit(1); } // get string from user cout << "Please enter a string: \n"; cin >> str; return str; } /**************************************************************************** Function: sort Purpose: This function will sort a given string in ascending order and return the sorted string Parameters: char pointer to original string Return Value: char pointer to sorted string ****************************************************************************/ void sort(char *s, char *d){ size_t len = strlen(s); // copy string s to string d strcpy(d,s); // sort string d for(size_t i = 0; i < len-1; i++) for(size_t j = len-1; j > i; --j){ if(d[j] < d[j-1]){ char temp; temp = d[j]; d[j] = d[j-1]; d[j-1] = temp; } } cout << "\nOriginal string s is: " << s << "\nSorted string d is: "<< d << endl; } /**************************************************************************** Function: strcmp Purpose: This function will compare two given strings and return 1, if S1 > S2 0, if S1 == S2 -1, if S1 < S2 Parameters: Two char pointers Return Value: int ****************************************************************************/ int strcmp(char *s1, char *s2){ short *asc1, *asc2; int cmp = 0; size_t len; size_t len1 = strlen(s1); // get length of 1st string size_t len2 = strlen(s2); // get length of 2nd string asc1 = new short; if(asc1 == NULL){ cout << "\nERROR - Insufficient memory\n"; exit(1); } asc2 = new short; if(asc2 == NULL){ cout << "\nERROR - Insufficient memory\n"; exit(1); } // get shortest length of two strings if (len1<=len2) len = len1; else len = len2; // convert both strings to ascii strings for (size_t i = 0; i asc2[i]) //S1 is greater than S2 cmp = 1; else if (asc1[i] < asc2[i]) //S1 is less than S2 cmp = (-1); else cmp = 0; i++; }while((asc1[i] == asc2[i] || i <= len) && cmp == 0); // all ascii codes are equal up to shortest length // check to see if the lengths are the same or not and // return appropriate value if (cmp == 0){ if (len1>len2) cmp = 1; else if (len1= 0; --i){ reverse[j] = s[i]; j++; } reverse[j] = '\0'; // add NULL charcter to reversed string cout << "\nThe original string is: " << s << "\nThe reverse string is: " << reverse << endl; // check if s is a palindrome // if string s is equal to reverse, then s is a palindrome for (i = 0; i