forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex6_16_17_18.cpp
123 lines (104 loc) · 2.9 KB
/
ex6_16_17_18.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
119
120
//!@Alan
//!
//! Exercise 6.16: The following function, although legal, is less useful
//! than it might be. Identify and correct the limitation on this function:
//! bool is_empty(string& s) { return s.empty(); }
//!
// Since this function doesn't change the argument,"const" shoud be added
// before string&s,otherwise this function is misleading and can't be used
// with const string or in a const function.
//!
//! Exercise 6.17:
//! Write a function to determine whether a string contains any capital letters.
//! Write a function to change a string to all lowercase.
//! Do the parameters you used in these functions have the same type? If so,
//! why? If not, why not?
//!
// Not the same.
// For the first one "const" was used, since no change need to do for the argument.
// For the second function,"const" can't be used,because the content of the agument
// should be changed.
//!
//! Exercise 6.18:
//! Write declarations for each of the following functions.
//! When you write these declarations, use the name of the function to indicate what
//! the function does.
//! (a)
//! A function named compare that returns a bool and has two parameters that are
//! references to a class named matrix.
// bool compare(const Matrix& _m1, const Matrix& _m2);
//!
//! (b)
//! A function named change_val that returns a vector<int> iterator and takes two
//! parameters: One is an int and the other is an iterator for a vector<int>.
// See below.
//!
#include <iostream>
#include <string>
#include <stdexcept>
#include <vector>
using namespace std;
//!
//! \brief for Exercise 6.18.
//! \param _i int
//! \param _vi vector<int>::iterator
//! \return vector<int>::iterator
//!
vector<int>::iterator change_val(int _i, vector<int>::iterator _vi);
//!
//! \brief for Exercise 6.17
//!
bool is_anyCapital_usingPointer(const string& s);
bool is_anyCapital_usingIterator(const string& s);
void lowercaseAstring(string& s);
//!
//! \brief for Exercise 6.16:
//!
bool is_empty(string& s);
int main()
{
string str;
cout<<"\nPlease enter:\n";
while(cin>>str)
{
lowercaseAstring(str);
cout<<"The lowercase=\n"
<<str;
}
return 0;
}
bool is_anyCapital_usingIterator(const string& s)
{
//!note: here must be "string::const_iterator"
//! rather than "string::iterator"
for(string::const_iterator it = s.begin(); it != s.end(); ++it)
{
if(std::isupper(*it))
{
return true;
}
}
return false;
}
bool is_anyCapital_usingPointer(const string& s)
{
for(const char* p=&s[0];p != &s[s.size()]; p++)
{
if(std::isupper(*p))
{
return true;
}
}
return false;
}
bool is_empty(string& s)
{
return s.empty();
}
void lowercaseAstring(string& s)
{
for(std::string::iterator it = s.begin(); it != s.end(); ++it)
{
*it = std::tolower(*it);
}
}