// 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 // Provides cout and cin #include // Provides EXIT_SUCCESS using 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 : Insert in the string array" << endl; cout << " ? : Is 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 : Remove from the string array" << endl; cout << " < : Print all strings from the string array that are smaller than " << endl; cout << " S : Substitute one occurrence of with " << endl; cout << " A : Substitute all occurrences of with " << 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; }