aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsabellaMon <[email protected]>2021-11-16 19:20:11 -0800
committerGitHub <[email protected]>2021-11-16 19:20:11 -0800
commitcc06b1e18d95b869c1ce9ef813c085d5a278012c (patch)
tree934bb25a9a02a358b4d1ef507ed6622a8b4a5ea6
parentAdd online IDE url (diff)
downloadcst116-lab7-isabellamon-cc06b1e18d95b869c1ce9ef813c085d5a278012c.tar.xz
cst116-lab7-isabellamon-cc06b1e18d95b869c1ce9ef813c085d5a278012c.zip
Add files via uploadHEADmaster
Lab7
-rw-r--r--M7L7Mon.txt459
1 files changed, 459 insertions, 0 deletions
diff --git a/M7L7Mon.txt b/M7L7Mon.txt
new file mode 100644
index 0000000..0073642
--- /dev/null
+++ b/M7L7Mon.txt
@@ -0,0 +1,459 @@
+CST116
+Module 7: Lab 7
+
+
+12a Write a program that includes a function to read (at least 100) strings into an array.
+Allow the user to either add a string or print out the contents of the array.
+Be sure to stop �printing� at the end of the entered values -- don�t �print� after the end of the user�s entered data.
+Be sure to give the user the chance to leave the program.
+10 pts Submit: code & runs
+
+CODE:
+ #include <iostream>
+ #include <cstring>
+ #include <string>
+
+ using namespace std;
+
+ #define ARRAY_SIZE 100
+
+ void GetInput(int& choice, int& count, string[ARRAY_SIZE]);
+ void Options(int& choice, int& count, string[ARRAY_SIZE]);
+
+ int main()
+ {
+ string stringArray[ARRAY_SIZE]{};
+ int choice = 0;
+ int count = 0;
+
+ GetInput(choice, count, stringArray);
+ }
+
+ void GetInput(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+ cout << "\nEnter a word: ";
+ cin >> stringArray[count];
+ count++;
+
+ Options(choice, count, stringArray);
+ }
+
+ void Options(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+ cout << "\n\n\t1. Enter another word ";
+ cout << "\n\t2. Print current words ";
+ cout << "\n\t3. Exit ";
+
+ cout << "\n\nWhat would you like to do next? ";
+ cin >> choice;
+
+ switch (choice)
+ {
+ case 1:
+ GetInput(choice, count, stringArray);
+ break;
+ case 2:
+ for (int i = 0; i < count; i++)
+ {
+ cout << stringArray[i] << "\n";
+ }
+ break;
+ case 3:
+ cout << "\n\tGoodbye :) " << endl;
+ break;
+ default:
+ cout << "\n\tInvalid option entered." << endl;
+ }
+ }
+
+RUN 1:
+
+ Enter a word: bella
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Exit
+
+ What would you like to do next? 3
+
+ Goodbye :)
+
+RUN 2:
+
+ Enter a word: hello
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Exit
+
+ What would you like to do next? 1
+
+ Enter a word: what
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Exit
+
+ What would you like to do next? 2
+ hello
+ what
+
+
+12b Add to your program from part 12a:
+An option in a function that can find a string or substring in a string in the array.
+This need just be the first occurrence of the string or substring.
+10 pts Submit: code & runs
+
+CODE:
+
+ #include <iostream>
+ #include <cstring>
+ #include <string>
+
+ using namespace std;
+
+ #define ARRAY_SIZE 100
+
+ void GetInput(int& choice, int& count, string[ARRAY_SIZE]);
+ void Options(int& choice, int& count, string[ARRAY_SIZE]);
+ void Find(int& choice, int& count, string[ARRAY_SIZE]);
+
+
+ int main()
+ {
+ string stringArray[ARRAY_SIZE]{};
+ int choice = 0;
+ int count = 0;
+
+ GetInput(choice, count, stringArray);
+ }
+
+ void GetInput(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+ cout << "\nEnter a word: ";
+ cin >> stringArray[count];
+ count++;
+
+ Options(choice, count, stringArray);
+ }
+
+ void Options(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+ cout << "\n\n\t1. Enter another word ";
+ cout << "\n\t2. Print current words ";
+ cout << "\n\t3. Look for a substring ";
+ cout << "\n\t4. Exit ";
+
+ cout << "\n\nWhat would you like to do next? ";
+ cin >> choice;
+
+ switch (choice)
+ {
+ case 1:
+ GetInput(choice, count, stringArray);
+ break;
+ case 2:
+ for (int i = 0; i < count; i++)
+ {
+ cout << stringArray[i] << "\n";
+ }
+ break;
+ case 3:
+ Find(choice, count, stringArray);
+ case 4:
+ cout << "\n\tGoodbye :) " << endl;
+ break;
+ default:
+ cout << "\n\tInvalid option entered." << endl;
+ }
+ }
+
+ void Find(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+
+ string subString{};
+
+ int i = 0, j = 0, k = 0;
+
+ cout << "What do you want to look for? " << endl;
+ cin >> subString;
+
+ while ((subString[i] != stringArray[0][j]) && (j < stringArray[count].size()))
+ j++;
+ if (subString[i] == stringArray[0][j])
+ while ((subString[i] == stringArray[0][j]) && (i < subString.size())
+ && j < stringArray[0].size())
+ {
+ i++;
+ j++;
+ }
+ if (i == subString.size())
+ cout << "\nSubstring " << subString << " was found.";
+ else
+ cout << "\nSubstring " << subString << " was not found.";
+ }
+
+RUN 1:
+
+ Enter a word: hello
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Exit
+
+ What would you like to do next? 3
+ What do you want to look for?
+ he
+
+ Substring he was found.
+
+RUN 2:
+
+ Enter a word: hello
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Exit
+
+ What would you like to do next? 3
+ What do you want to look for?
+ hi
+
+ Substring hi was not found.
+
+
+11c Add to your program from part 12b:
+An option in a function that can remove a string from the array (and compress the array) based on its location in the array.
+Make sure that the �print� option reflects the removed string.
+10 pts Submit: code & runs
+Total: 30 pts
+
+CODE:
+
+ #include <iostream>
+ #include <cstring>
+ #include <string>
+
+ using namespace std;
+
+ #define ARRAY_SIZE 100
+
+ void GetInput(int& choice, int& count, string[ARRAY_SIZE]);
+ void Options(int& choice, int& count, string[ARRAY_SIZE]);
+ void Find(int& choice, int& count, string[ARRAY_SIZE]);
+ void Delete(int& choice, int& count, string[ARRAY_SIZE]);
+ void Print(int& choice, int& count, string[ARRAY_SIZE]);
+
+ int main()
+ {
+ string stringArray[ARRAY_SIZE]{};
+ int choice = 0;
+ int count = 0;
+
+ GetInput(choice, count, stringArray);
+ }
+
+ void GetInput(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+ cout << "\nEnter a word: ";
+ cin >> stringArray[count];
+ count++;
+
+ Options(choice, count, stringArray);
+ }
+
+ void Options(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+ cout << "\n\n\t1. Enter another word ";
+ cout << "\n\t2. Print current words ";
+ cout << "\n\t3. Look for a substring ";
+ cout << "\n\t4. Delete a word ";
+ cout << "\n\t5. Exit ";
+
+ cout << "\n\nWhat would you like to do next? ";
+ cin >> choice;
+
+ switch (choice)
+ {
+ case 1:
+ GetInput(choice, count, stringArray);
+ break;
+ case 2:
+ Print(choice, count, stringArray);
+ break;
+ case 3:
+ Find(choice, count, stringArray);
+ case 4:
+ Delete(choice, count, stringArray);
+ case 5:
+ cout << "\n\tGoodbye :) " << endl;
+ break;
+ default:
+ cout << "\n\tInvalid option entered." << endl;
+ cout << "\n\tTry again: " << endl;
+
+ Options(choice, count, stringArray);
+ }
+ }
+
+ void Find(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+
+ string subString{};
+
+ int i = 0, j = 0, k = 0;
+
+ cout << "What do you want to look for? " << endl;
+ cin >> subString;
+
+ while ((subString[i] != stringArray[0][j]) && (j < stringArray[count].size()))
+ j++;
+ if (subString[i] == stringArray[0][j])
+ while ((subString[i] == stringArray[0][j]) && (i < subString.size())
+ && j < stringArray[0].size())
+ {
+ i++;
+ j++;
+ }
+ if (i == subString.size())
+ cout << "\nSubstring " << subString << " was found.";
+ else
+ cout << "\nSubstring " << subString << " was not found.";
+
+ Options(choice, count, stringArray);
+ }
+
+ void Delete(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+ int Pos = 0;
+ int i = 0;
+
+ cout << "What position is the word you want to remove in? ";
+ cin >> Pos;
+ Pos--;
+ for (int i = Pos; i < count; ++i)
+ stringArray[i] = stringArray[i + 1];
+ Options(choice, count, stringArray);
+ }
+
+ void Print(int& choice, int& count, string stringArray[ARRAY_SIZE])
+ {
+ for (int i = 0; i < count; i++)
+ {
+ cout << stringArray[i] << "\n";
+ }
+ Options(choice, count, stringArray);
+ }
+
+RUN 1:
+
+ Enter a word: hello
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Delete a word
+ 5. Exit
+
+ What would you like to do next? 1
+
+ Enter a word: world
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Delete a word
+ 5. Exit
+
+ What would you like to do next? 4
+ What position is the word you want to remove in? 2
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Delete a word
+ 5. Exit
+
+ What would you like to do next? 2
+ hello
+
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Delete a word
+ 5. Exit
+
+ What would you like to do next? 5
+
+ Goodbye :)
+
+RUN 2:
+
+ Enter a word: Isabella
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Delete a word
+ 5. Exit
+
+ What would you like to do next? 1
+
+ Enter a word: Jorahnie
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Delete a word
+ 5. Exit
+
+ What would you like to do next? 1
+
+ Enter a word: Mon
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Delete a word
+ 5. Exit
+
+ What would you like to do next? 4
+ What position is the word you want to remove in? 3
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Delete a word
+ 5. Exit
+
+ What would you like to do next? 2
+ Isabella
+ Jorahnie
+
+
+
+ 1. Enter another word
+ 2. Print current words
+ 3. Look for a substring
+ 4. Delete a word
+ 5. Exit
+
+ What would you like to do next? 5
+
+ Goodbye :)