// FILE: str_demo.cxx// Written by Shivakant Mishra// Last Update: February 22, 2008// This is a demonstration program showing how the string functions are used.#include <iostream> // Provides cout and cin#include <cstdlib> // Provides EXIT_SUCCESSusing namespace std;void print_menu( );char get_user_command( );typedef char* mystring; int main( ){ mystring *str; //Array of strings; size_t count = 0; cout << "Enter the size of your string array (an integer > 0): "; int sz; cin >> sz; assert (sz > 0); str = new mystring[sz]; //str is an array of size sz cout << "I have initialized an empty string array of size" << sz << endl; char choice, s1[100], s2[100]; int i, cn = 0; bool flag = false; do { print_menu( ); choice = toupper(get_user_command( )); switch (choice) { case 'I': cin >> s1; if (count != sz) { str[count] = new char[strlen(s1) + 1]; strcpy(str[count], s1); count++; cout << "String <" << s1 << "> has been inserted in the string array" << endl; } else cout << "String array is full" << endl; break; case '?': cin >> s1; for (i = 0; i < count; i++) if (strcmp(str[i], s1) == 0) { cout << "String <" << s1 << "> is present in the string array" << endl; break; } if (i == count) cout << "String <" << s1 << "> is not present in the string array" << endl; break; case 'C': cout << "There are " << count << " strings in the string array" << endl; break; case 'P': for (i = 0; i < count; i++) cout << str[i] << endl; break; case 'R': cin >> s1; for (i = 0; i < count; i++) { if (strcmp(str[i], s1) == 0) { delete [] str[i]; str[i] = str[count-1]; count--; cout << "One occurrence of <" << s1 << "> has been removed" << endl; flag = true; break; } } if (!flag) cout << "String <" << s1 << "> is not present in the string array" << endl; flag = false; break; case '<': cin >> s1; cout << "Strings in the string array that are less than <" << s1 << "> are:" << endl; for (i = 0; i < count; i++) if (strcmp(str[i], s1) < 0) cout << str[i] << endl; break; case 'S': cin >> s1 >> s2; for (i = 0; i < count; i++) if (strcmp(str[i], s1) == 0) { delete [] str[i]; str[i] = new char[strlen(s2) + 1]; strcpy(str[i], s2); cout << "One occurrence of <" << s1 << "> has been substituted by <" << s2 << ">" << endl; break; } if (i == count) cout << "String <" << s1 << "> is not present in the string array" << endl; break; case 'A': cin >> s1 >> s2; for (i = 0; i < count; i++) if (strcmp(str[i], s1) == 0) { delete [] str[i]; str[i] = new char[strlen(s2) + 1]; strcpy(str[i], s2); cn++; } cout << cn << " occurrence of <" << s1 << "> has/have been substituted by <" << s2 << ">" << endl; cn = 0; break; case 'Q': cout << "The demo program has ended." << endl; break; default: cout << choice << " is invalid." << endl; } } while ((choice != 'Q')); return EXIT_SUCCESS; }void print_menu( )// Library facilities used: iostream.h{ cout << endl; // Print blank line before the menu cout << "The following choices are available (A string ends with a white space): " << endl; cout << " I <string> : Insert <string> in the string array" << endl; cout << " ? <string> : Is <string> available in the string array?" << endl; cout << " C : Print the number of strings in the string array" << endl; cout << " P : Print all strings in the string array" << endl; cout << " R <string> : Remove <string> from the string array" << endl; cout << " < <string> : Print all strings from the string array that are smaller than <string>" << endl; cout << " S <string1> <string2> : Substitute one occurrence of <string1> with <string2>" << endl; cout << " A <string1> <string2> : Substitute all occurrences of <string1> with <string2>" << endl; cout << " Q : Quit this test program" << endl; cout << endl; // Print blank line after the menu}char get_user_command( )// Library facilities used: iostream{ char command; cout << "Enter choice: "; cin >> command; // Input of characters skips blanks and newline character return command;}