blob: fcebb9c9f0f679491994ed5996c8042052a52fa0 (
plain)
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
|
#include <iostream>
#include <cctype>
/*
*
* 6.17
*
*
*/
bool contains_captial(const std::string &cs) {
bool cc = false;
for(auto a : cs) {
if(isupper(a)) {
cc = true;
break;
}
}
return cc;
}
void tolower_str(std::string &cs) {
for(auto &a : cs) {
a = tolower(a);
}
}
std::string tolower_str_literals_incl(const std::string &cs) {
std::string ret = cs;
for(auto &a : ret) {
a = tolower(a);
}
return ret;
}
int main () {
/*
They do need to have different types. With contains_capital we only need to read
the string and tell if it has an uppercase or not.
With tolower we actually need to change a string. Or perhaps return a lowercase version.
This does mean that we can't use string literals as arguments. Well... If we use a reference.
We don't actually NEED to use a reference if we really wanted to use literals as well.
Because then we need a different approach for the function.
There are several different ways we can define this function really.
*/
std::cout << contains_captial("hello") << std::endl;
std::cout << contains_captial("Hello") << std::endl;
std::string aa = "HELLO!!!";
std::cout << aa << std::endl;
tolower_str(aa);
std::cout << aa << std::endl;
std::string aa2 = "WOWZA!";
std::string aa3 = tolower_str_literals_incl(aa2);
std::cout << aa3 << std::endl;
std::cout << tolower_str_literals_incl("HIIII!") << std::endl;
return 0;
}
|