1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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";
}
|