-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab.cpp
118 lines (94 loc) · 4.25 KB
/
lab.cpp
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
/*****************************************************************//**
* \file lab.cpp
* \brief lab on strings in C++
*
* \author Xuhua Huang
* \date February 2021
*********************************************************************/
#include <iostream>
#include <string>
int main(void) {
// requirement 1
std::string userInput1;
std::cout << "Please enter your favourite idiom:" << "\n";
getline(std::cin, userInput1);
std::cout << "The sentence you just entered is: " + userInput1 << "\n"; // for testing purpose only
std::string dogName;
std::cout << "Please enter your dog's name. We promise we won't do any harm to your loved ones." << "\n";
std::cin.ignore() >> dogName;
std::string dogBreed;
std::cout << "Do you mind telling us its breed?" << "\n";
std::cin.ignore() >> dogBreed;
std::cout << "Great! " + dogName + " is of breed " + dogBreed << "\n";
std::cout << "This is the end of requirement 1." << "\n";
// requirement 2 find and erase
std::string fixedStr = "she sells seashells from the sea shore.";
size_t search = fixedStr.find('s', 0);
while (search != std::string::npos) {
fixedStr.erase(search, 1);
search = fixedStr.find('s', 0);
std::cout << "It is now: " + fixedStr << "\n"; // printing the lines to see one "s" less each line
} // end while
// requirement 3: insert letter "q" to the center of any word
std::cout << "\nWarning! I am going to insert letter \"q\" to the center of your std::string." << "\n";
std::string userInput2;
std::cin >> userInput2;
int strlength = userInput2.length();
std::cout << "\nThe size of the word you just entered is: " << strlength << "\n"; // for testing purpose
int halflength = strlength / 2;
if (halflength % 2 == 1)
userInput2.insert(halflength + 1, 1, 'q');
else if (halflength % 2 == 0)
userInput2.insert(halflength, 1, 'q');
std::cout << "New \"word\" is: " << userInput2 << "\n";
std::cout << "\nHow many \"q\"s do you want to insert to the center of the word?" << "\n";
int numq = 0;
std::cin >> numq;
userInput2.insert(halflength, numq - 1, 'q'); // there was already one q existed
std::cout << "Now the new \"word\" is: " << userInput2 << "\n";
// requirement 5 using iterators
std::cout << "Using iterators..." << "\n";
std::string alpha = "abcdefghijklmnopqrstuvwxyz";
std::cout << "The original alphabetical std::string is: " << alpha << "\n";
std::string::iterator iteralpha; // define an iterator object
for (int x = 0; x <= (int)alpha.length(); x++) // forstd::cing it to of type int
{
for (iteralpha = alpha.begin(); iteralpha < alpha.begin() + x; iteralpha++) {
std::cout << *iteralpha;
}
std::cout << "\n";
}
// requirement 6 comparing std::strings
std::cout << "Comparing std::string..." << "\n";
std::string fruit1, fruit2, fruit3;
std::cout << "Please enter three different fruits: " << "\n";
std::cout << "\nThe first one:\n";
std::cin >> fruit1;
std::cout << "The second one:\n";
std::cin >> fruit2;
std::cout << "The second one:\n";
std::cin >> fruit3;
if ((fruit1 < fruit2) && (fruit1 < fruit3)) // fruit1 comes first
{
std::cout << "\n" << fruit1 << " comes first alphabetically." << "\n";
if (fruit2 < fruit3)
std::cout << fruit3 << " comes least alphabetically." << "\n";
else // fruit3 < fruit2
std::cout << fruit2 << " comes least alphabetically." << "\n";
} else if ((fruit2 < fruit1) && (fruit2 < fruit3)) // fruit2 comes first
{
std::cout << "\n" << fruit2 << " comes first alphabetically." << "\n";
if (fruit1 < fruit3)
std::cout << fruit3 << " comes least alphabetically." << "\n";
else // fruit3 < fruit1
std::cout << fruit1 << " comes least alphabetically." << "\n";
} else // fruit3 comes first
{
std::cout << "\n" << fruit3 << " comes first alphabetically." << "\n";
if (fruit1 < fruit2)
std::cout << fruit2 << " comes least alphabetically." << "\n";
else // fruit2 < fruit1
std::cout << fruit1 << " comes least alphabetically." << "\n";
}
return 0;
}