aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab7/Lab7-Billingsley.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'CST116F2021-Lab7/Lab7-Billingsley.cpp')
-rw-r--r--CST116F2021-Lab7/Lab7-Billingsley.cpp181
1 files changed, 181 insertions, 0 deletions
diff --git a/CST116F2021-Lab7/Lab7-Billingsley.cpp b/CST116F2021-Lab7/Lab7-Billingsley.cpp
new file mode 100644
index 0000000..c571ef0
--- /dev/null
+++ b/CST116F2021-Lab7/Lab7-Billingsley.cpp
@@ -0,0 +1,181 @@
+// File: Lab7-Billingsley.cpp
+// Summary: The completed lab 7 program. Code added in 12b and 11c are marked
+// Author: Logan Billingsley
+// Date: 11/16/2021
+#include <iostream>
+#include <cstring>
+#include <string> // for getine
+
+using namespace std;
+
+const int STRING = 100; // Max number of strings
+
+void enterString(string[], int&);
+void printString(string[], int);
+void findString(string[], int); // Added code from 12b
+void findSub(string[], int); // Added code from 12b
+void deleteString(string[], int&); // Added code from 11c
+
+int main()
+{
+ string list[STRING];
+ int size = 0;
+ int menu; // Menu Choice
+ string repeat; // Used in looping menu
+
+ enterString(list, size);
+
+ do
+ {
+ cout << "Menu:\n"
+ << "1: Enter strings\n"
+ << "2: Print strings\n"
+ << "3: Find string\n" // Added code from 12b
+ << "4: Find sub-string\n" // Added code from 12b
+ << "5: Delete string\n" // Added code from 11c
+ << "6: exit\n\n";
+ cin >> menu;
+ cin.ignore(180, '\n'); // Used here so future getline functions work
+ cout << endl;
+ switch (menu)
+ {
+ case 1:
+ enterString(list, size);
+ break;
+ case 2:
+ printString(list, size);
+ break;
+ case 3: // Added code from 12b
+ findString(list, size);
+ break;
+ case 4: // Added code from 12b
+ findSub(list, size);
+ break;
+ case 5: // Added code from 11c
+ deleteString(list, size);
+ break;
+ default:
+ cout << "Exiting program\n";
+ return 0;
+ }
+ cout << "Do you wish to select another program?\n"
+ << "Please input \"Yes\" or \"No\": ";
+ cin >> repeat;
+ cout << endl;
+ } while (repeat == "yes" || repeat == "Yes" || repeat == "YES"); // While selected phrase to repeat is entered
+
+ return 0;
+}
+void enterString(string list[], int& size)
+{
+ cout << "Please enter each string separated by a newline.\n"
+ << "Once you want to stop inputting strings, type \"exit\"\n\n";
+ for (int i = size; i < STRING; i++) // Repeat until the limit (100 strings)
+ {
+ size = i; // Set size = i after increasing it. I would just use size in the for loop, but that caused problems.
+ cout << i + 1 << ":\t\b\b\b"; // I would rather do setw(4) however that is right-aligned
+ getline(cin, list[i], '\n');
+ if (list[i] == "exit" || list[i] == "Exit" || list[i] == "EXIT") // Exit if input string is selected exit phrase
+ break;
+ }
+ if (size == 99) // 0-99 is the 100 strings, therefor 99 is max.
+ cout << "\nSize limit reached\n";
+ cout << "Returning to menu\n\n";
+}
+void printString(string list[], int size)
+{
+ cout << "Printing inputted strings:\n\n";
+
+ for (int i = 0; i < size; i++) // For all input strings
+ {
+ cout << i + 1 << ":\t\b\b\b" << list[i] << endl; // I would rather do setw(4) however that is right-aligned
+ }
+}
+void findString(string list[], int size) // Added code from 12b
+{
+ int found = 0; // Number of strings found
+ string search[1]; // String
+ cout << "Please enter string you want to search for:\n\n";
+ getline(cin, search[0], '\n');
+ for (int i = 0; i < size; i++) // Search each string
+ {
+ if (list[i] == search[0]) // If strings match
+ {
+ cout << "Found string " << i + 1 << endl;
+ found++; // Add 1 to number of strings found
+ }
+ }
+ if (found == 1) // Formatting so grammatically correct
+ cout << "Found 1 string.\n\n";
+ else
+ cout << "Found " << found << " strings.\n\n";
+}
+void findSub(string list[], int size) // Added code from 12b
+{
+ int found = 0; // Number of cases found
+ string search[1]; // Substring
+ cout << "Please enter substring you want to search for:\n\n";
+ getline(cin, search[0], '\n'); // Get substring
+ for (int i = 0; i < size; i++) // Search each string
+ {
+ int k = 0; // Set the current substring to 0;
+ int n = 0; // Set number of substrings found per string to 0
+ for (int j = 0; j < list[i].size(); j++) // Search each character in the current string
+ {
+ if (list[i][j] == search[0][k]) // If current string character is equal to the current substring character
+ {
+ k++; // Add to characted matched
+ if (k == search[0].size()) // If character all characters match
+ {
+ found++; // Add to number of cases found
+ k = 0; // Reset current substring character to search more in that string
+ n++; // Add 1 to the number of substrings found per string
+ }
+ }
+
+ }
+ if (n > 0) // If found substring in string
+ {
+ if (n == 1) // Formatting so grammatically correct
+ cout << "Found 1 instance of the substring: \"" << search[0] << "\" in the string: \"" << list[i] << "\"\n\n";
+ else
+ cout << "Found " << n << " instances of the substring: \"" << search[0] << "\" in the string: \"" << list[i] << "\"\n\n";
+ }
+ }
+ if (found == 1) // Formatting so gramatically correct
+ cout << "Found 1 instance of the substring: \"" << search[0] << "\"\n\n";
+ else
+ cout << "Found " << found << " instances of the substring: \"" << search[0] << "\"\n\n";
+}
+void deleteString(string list[], int& size) // Added code from 11c
+{
+ int del; // Which string to delete 1-100
+ bool pass = 0; // If check is passed
+ cout << "Select which string you want to delete\n"
+ << "Please enter the number of that string. If none, enter \"0\": ";
+ do
+ {
+ cin >> del;
+ cout << endl;
+ if (del >= 0 && del <= size) // If string selected is within range
+ pass = 1;
+ else
+ {
+ cout << "Invalid number.\n"
+ << "Please try again: ";
+ }
+ } while (pass == 0); // If check is passed
+ if (del == 0) // Exit if input string is selected exit phrase
+ {
+ cout << "Returning to Menu\n\n";
+ return;
+ }
+ del--; // Convert to counting from 0
+ cout << "Deleting string number " << del + 1 << ": " << list[del] << endl << endl;
+ for (int i = del; i < size; i++)
+ {
+ list[i] = list[i + 1]; // Make each string equal the one after it (effectively erasing it)
+ }
+ size--; // Remove the empty string from the end of the array
+ cout << "Returning to Menu\n\n";
+} \ No newline at end of file