aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab7/CST116F2021-Lab7.cpp
blob: 66296d7c120bc69bb6c14bd6730c4e1d60ef2311 (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
CST 116 Austin Guertin
Module 7: Lab 7
  
  12a
  
  #include <iostream>
#include <string>

using namespace std;

//function to read input
void read(string str[], int idx)
{
	getline(cin, str[idx]);
}

int main()
{

	string str[100];
	int choice, size = 0;


	while (1)
	{
	
		cout << "\nPress 1: To add a string\n";
		cout << "Press 2: To print the strings\n";
		cout << "Press 3: Exit the program\n\n";
		cout << "Enter your choice down below: ";
		cin >> choice;

	
		if (choice == 1)
		{
			cin.ignore(80, '\n');
			cout << "\nEnter a string: ";
			read(str, size++);
		}


		else if (choice == 2)
			break;


		else if (choice == 3)
			return 0;
		else
			cout << "\n\nInvalid input!\n\n";
	}


	cout << endl << "The input strings are: \n\n";
	for (int i = 0; i < size; i++)
	{
		if (str[i] == "don't print")
			break;
		cout << str[i] << endl;
	}

	return 0;
}
  
  12b
  
  #include <iostream>
#include <string>

using namespace std;

void read(string str[], int idx)
{
	getline(cin, str[idx]);
}

int main()
{
	string str[100];
	int choice, choice2, size = 0;
	string FindS;

	while (1)
	{
		cout << "\nPress 1: To add a string\n";
		cout << "Press 2: To print the strings\n";
		cout << "Press 3: Exit the program\n\n";
		cout << "Enter your choice down below: ";
		cin >> choice;

		if (choice == 1)
		{
			cin.ignore(80, '\n');
			cout << "\nEnter a string: ";
			read(str, size++);
		}

		else if (choice == 2)
			break;

		else if (choice == 3)
			return 0;
		else
			cout << "\n\nInvalid input!\n\n";
	}

	cout << endl << "The input strings are: \n\n";
	for (int i = 0; i < size; i++)
	{
		if (str[i] == "don't print")
			break;
		cout << str[i] << endl;
	}

	cout << "Would you like to find a specific string or substring in this array? ('1' for yes or '2' for no)\n";
	cin >> choice2;

	if (choice2 == 1)
	{
		cout << "\nWhat is the word you want to look for?\n\n";
		cin >> FindS;

		for (int i = 0; i < size; i++)
		{
			size_t found = str[i].find(FindS);
			if (found != std::string::npos)
				cout << "\nThe word " << FindS << " starts at position: " << found + 1 << "\n\n";
		}

		return 0;
	}
}
  
  11c