-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.md.html
147 lines (107 loc) · 4.2 KB
/
5.md.html
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
<meta charset="utf-8">
**Strings**
You probably took strings for granted in CS11. That's because strings are so powerful in Python; as the primary way to transmit text, we used it generously and intuitively in our code.
But there's really no "native" way for computers to store characters. After all, computers only really understand numbers, and nothing else. Therefore, it's the programming language's job to find a way to better represent text in such a way that you can easily use it and the computer can easily process it. We'll be discussing two types of strings that C++ can process; the `string` class and the C-string.
# `char`
First, let's discuss the built-in `char` type built into C++ (and C, as an extension). The `char` type can be used to store single characters.
<div class="split">
<div>
**Code**
```cpp
#include <iostream>
using namespace std;
int main() {
char ans;
cout << "Do you want to continue? (Y/N) > ";
cin >> ans;
if (ans == 'Y') {
cout << "Sure, you can continue!" << endl;
} else if (ans == 'N') {
cout << "Goodbye then!" << endl;
} else {
cout << "Sorry, I don't know what that means." << endl;
}
return 0;
}
```
</div>
<div>
**Output**
```markdown
Do you want to continue? (Y/N) > N
Goodbye then!
```
</div>
</div>
A char literal is demarked by single quotes `''`; this separates it from the strings we've dealt with before, which are demarked by double quotes `""`.
Internally, `char` is nothing more than a single-byte integer. Each integer is mapped by C++ to a certain character using the ASCII representation, which includes most (if not all) of the characters on your keyboard (and some "control characters" like the newline and tab).
![The ASCII character table. Image from Simple Wikipedia.](img/05-ascii-table.png)
Since `char`s are just integers, we can manipulate them just like integers, and we don't have to convert them like in Python. Here are some common ways of using them in C++:
* To check if a character is a number or letter, we can use the comparison operators.
```cpp
// is the char a lowercase letter?
'a' <= c && c <= 'z'
// is it uppercase?
'A' <= c && c <= 'Z'
// is it a number?
'0' <= c && c <= '9'
```
* Converting a numerical character to a number is as easy as doing this:
```cpp
if ('0' <= c && c <= '9') {
int i = c - '0';
}
```
* Determining the index of a letter from 0-25 is also easy:
```cpp
if ('a' <= c && c <= 'z') {
int i = c = 'a';
}
```
This is useful for ciphers and similar applications.
* In fact, since characters are just integers, we can use them to index through arrays:
```cpp
int map[128];
cout << map['a'] << endl; // corresponds to map[97]
```
# `std::string`
The first type we'll be discussing is a more C++-style way of representing integers: the `string` class. It's very similar to Python's `string` type, except it is mutable, i.e. easily modifiable.
<div class="split">
<div>
**Code**
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string
}
```
</div>
<div>
**Output**
```
[💩]
```
</div>
</div>
# `char`
# C-strings
<!-- C-strings are the more "bare-metal" representation of strings in C++. -->
<!-- NOTE: Neither C-strings nor std::strings are really "native" to C++. -->
# References and further reading
* https://www.learncpp.com/cpp-tutorial/27-chars/
<script>
window.ga = window.ga || function () { (ga.q = ga.q || []).push(arguments) }; ga.l = +new Date;
ga('create', 'UA-68097958-3', 'auto');
ga('send', 'pageview');
</script>
<script async src='//www.google-analytics.com/analytics.js'></script>
<script>
window.markdeepOptions = {
tocStyle: 'medium'
}
</script>
<style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://casual-effects.com/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/github-gist.min.css">