blob: c036c721e4f2e6b81327b955827364bfde4ec2a9 (
plain) (
blame)
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
|
// CST116F2021-Lab7.cpp : This file contains the 'main' function. Program execution begins and ends there
// ...from : Lab7_Schroeder.cpp
#include <iostream>
#include<iomanip>
#include<string>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
using namespace std;
#define ARRAY_SIZE 100
void menu(int&);
void readData(int&);
void printData(int&);
void findString(int&);
void deleteString(int&);
string stringArray[ARRAY_SIZE]{};
int item = 0;
int main()
{
cout << "*\t\t\tThis program will build an array of strings.\t\t\t* \n*\t\t\tChoose one of the 5 options to get started:\t\t\t*\n";
cout << "*****************************************************************************************\n";
menu(item);
}
void menu(int& item)
{
int menuChoice = 0;
cout << "MENU: 1) Add a string 2) Print out 3) Find subString 4) Delete a string 5) Exit \n";
cin >> menuChoice;
if (cin.fail())
{
menuChoice = 0;
cout << "(you have to enter: 1,2,3,4 or 5 (start the program again))";
cin >> menuChoice;
}
else
{
while ((menuChoice < 1 || menuChoice >5))
{
cout << "(enter: 1,2,3,4,or 5) ";
cin >> menuChoice;
}
}
switch (menuChoice)
{
case 1:
{
readData(item);
break;
}
case 2:
{
printData(item);
break;
}
case 3:
{
findString(item);
break;
}
case 4:
{
deleteString(item);
break;
}
case 5:
{
cout << "\n\tThankyou, Goodbye!!\n\n";
break;
}
}
}
void readData(int& item)
{
string tempString;
cout << "Type in your string to add: ";
getline(cin >> ws, tempString);
stringArray[item] = tempString;
item++;
menu(item);
}
void printData(int& item)
{
cout << "\n\nHere are your string entries...\n";
for (int i = 0; i < item; i++)
{
cout << i + 1 << ".\t" << stringArray[i] << "\n";
}
cout << "\n\n";
menu(item);
}
void findString(int& item)
{
string temp = " ";
cout << "Enter the subString you want to search for: ";
cin >> temp;
string subString{ temp };
for (int k = 0; k <= item - 1; k++)
{
int i = 0, j = 0;
while ((subString[i] != stringArray[k][j]) && (j < stringArray[k].size()))
{
j++;
}
if (subString[i] == stringArray[k][j])
while ((subString[i] == stringArray[k][j]) && (i < subString.size()) && j < stringArray[k].size())
{
i++;
j++;
}
if (i == subString.size())
cout << "\tSubstring '" << subString << "' was found in string: " << k + 1 << "\n";
else
cout << "\t...not found in string: " << k + 1 << "\n";
}
cout << "\n\n";
menu(item);
}
void deleteString(int& item)
{
int temp = 0;
cout << "Which string (1 to " << item << ") would you like to delete? ";
cin >> temp;
int i = temp - 1;
stringArray[i] = {};
for (i; i <= item - 1; i++)
{
stringArray[i] = stringArray[i + 1];
}
item--;
cout << "\n\n";
menu(item);
}
|