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
|
// 12a.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <string>
using namespace std;
#define ARRAY_SIZE 100
#define EXIT 5
void GetChoice(int& choice);
void PrintList(string strs[ARRAY_SIZE], int& strct);
void FindSubstring(string strs[ARRAY_SIZE], int& strct);
void RemoveItem(string strs[ARRAY_SIZE], int& strct);
void AddStr(string strs[ARRAY_SIZE], int& strct);
int main()
{
string strs[ARRAY_SIZE]{};
string substring;
int strct = 0, choice = 0;
while (choice != EXIT)
{
GetChoice(choice);
switch (choice)
{
case 1:
AddStr(strs, strct);
break;
case 2:
PrintList(strs, strct);
break;
case 3:
FindSubstring(strs, strct);
break;
case 4:
RemoveItem(strs, strct);
break;
case EXIT:
cout << "Exiting Program...";
break;
default:
cout << "Unknown choice: " << choice;
break;
}
}
}
void GetChoice(int& choice)
{
string menu = "\t\t--String program--\n\
\t1. Add a string\n\
\t2. Print out string list\n\
\t3. Find a substring\n\
\t4. Remove an element\n\
\t5. Exit Program\n\n\
Input your choice: ";
cout << menu;
cin >> choice;
while (!cin)
{
cout << "Input was not an integer. Please try again." << endl;
cin.clear();
cin.ignore();
cout << menu;
cin >> choice;
}
}
void AddStr(string strs[ARRAY_SIZE], int& strct)
{
string newstr;
cout << "Input a string: ";
cin.clear();
cin.ignore();
getline(cin, newstr);
strs[strct++] = newstr;
cout << newstr << " added to list.\n";
}
void PrintList(string strs[ARRAY_SIZE], int& strct)
{
cout << "Outputing string list:" << endl;
for (int i = 0; i < strct; i++)
{
cout << strs[i] << endl;
}
}
void FindSubstring(string strs[ARRAY_SIZE], int& strct)
{
string search;
int start = 0;
cout << "Input the substring to search for: ";
cin.clear();
cin.ignore();
getline(cin, search);
for (int i = 0; i < strct; i++)
{
if (strs[i].find(search) != string::npos)
{
cout << search << " was found in string " << i + 1 << ": " << strs[i] << " at position " << strs[i].find(search) << endl;
return;
}
}
cout << search << " could not be found in the list." << endl;
}
void RemoveItem(string strs[ARRAY_SIZE], int& strct)
{
int loc;
cout << "Input the item to remove: ";
cin >> loc;
while (!cin || loc > strct || loc < 0)
{
if (!cin)
{
cout << "Please input a valid integer." << endl;
}
else if (loc > strct || loc < 1)
{
cout << "Please input a value between 1 and " << strct+1 << "." << endl;
}
cin.clear();
cin.ignore();
cout << "Input the item to remove: ";
cin >> loc;
}
cout << "Rmeoving item: " << loc << endl;
for (int i = loc-1; i < strct; i++)
{
strs[i] = strs[i + 1];
}
strct--;
}
|