|
| 1 | +# Characters and Strings |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +- Character functions |
| 6 | +- C-style Strings |
| 7 | +- Working with C-style Strings |
| 8 | +- C++ Strings |
| 9 | +- Working with C++ Strings |
| 10 | + |
| 11 | +## Character Functions |
| 12 | + |
| 13 | +```c++ |
| 14 | +#include <cctype> |
| 15 | + |
| 16 | +function_name (char) |
| 17 | +``` |
| 18 | +
|
| 19 | +- Functions for testing characters |
| 20 | +- Functions for converting character case |
| 21 | +
|
| 22 | +### Testing characters |
| 23 | +
|
| 24 | +| `isalpha(c)` | True if c is a letter | |
| 25 | +| `isalnum(c)` | True if c is a letter of digit | |
| 26 | +| `isdigit(c)` | True if c is a digit | |
| 27 | +| `islower(c)` | True if c is a lowercase letter | |
| 28 | +| `isprint(c)` | True if c is a printable character | |
| 29 | +| `ispunct(c)` | True if c is a punctuation character | |
| 30 | +| `isupper(c)` | True if c is an uppercase letter | |
| 31 | +| `isspace(c)` | True if c is whitespace | |
| 32 | +
|
| 33 | +### Converting characters |
| 34 | +
|
| 35 | +| `tolower(c)` | return lowercase of c | |
| 36 | +| `toupper(c)` | return uppercase of c | |
| 37 | +
|
| 38 | +## C-Style Strings |
| 39 | +
|
| 40 | +- Sequence of characters |
| 41 | + - Contiguous in memory |
| 42 | + - Implemented as an array of characters |
| 43 | + - Fixed in size |
| 44 | + - Terminated by a null character (null) |
| 45 | + - null - character with a value of zero |
| 46 | + - Referred to as zero or null terminated strings |
| 47 | +
|
| 48 | +- String literal |
| 49 | + - Sequence of characters in double quote |
| 50 | + - e.g. "Anthony" |
| 51 | + - Constant |
| 52 | + - Terminated with null character |
| 53 | +
|
| 54 | +Example |
| 55 | +`"C++ is fun"` is stored: |
| 56 | +| C | + | + | | | i | s| | f | u | n | \0 | |
| 57 | +- Needed null character in the end |
| 58 | +- size of 11 in memory instead of 10 |
| 59 | +
|
| 60 | +### Declaring variables |
| 61 | +
|
| 62 | +```c++ |
| 63 | +char my_name [] {"Frank"}; |
| 64 | +
|
| 65 | +my_name[5] = 'y'; // Problem since not dynamic size |
| 66 | +``` |
| 67 | + |
| 68 | +```c++ |
| 69 | +char my_name [8]; |
| 70 | +| ? | ? | ? | ? | ? | | ? | ? | ? | |
| 71 | + |
| 72 | +my_name = "Frank"; // Error |
| 73 | + |
| 74 | +strcpy(my_name, "Frank"); // OK |
| 75 | +``` |
| 76 | +
|
| 77 | +### Functions that work with C-style Strings |
| 78 | +
|
| 79 | +- Copying |
| 80 | +- Concatenation |
| 81 | +- Comparison |
| 82 | +- Searching |
| 83 | +- and others |
| 84 | +
|
| 85 | +All functions rely on one factor: that the sequence of characters that they are working on are terminated by the null character. |
| 86 | +
|
| 87 | +Example |
| 88 | +
|
| 89 | +```c++ |
| 90 | +char str[80]; |
| 91 | +
|
| 92 | +strcpy(str, "Hello "); // copy |
| 93 | +
|
| 94 | +strcat(str, "there"); // concatenate |
| 95 | +
|
| 96 | +cout << strlen(str); // length - Output: 11 |
| 97 | +
|
| 98 | +strcmp(str, "Another"); // compare - Ouput: 0 |
| 99 | +``` |
| 100 | + |
| 101 | +### General purpose functions |
| 102 | + |
| 103 | +- C standard library: `#include <cstdlibb>` |
| 104 | +- Includes functions to convert C-style Strings to |
| 105 | + - integer |
| 106 | + - float |
| 107 | + - long |
| 108 | + - etc. |
| 109 | + |
| 110 | +## C++ Strings |
| 111 | + |
| 112 | +- `std::string` is a Class in the Standard Template Library |
| 113 | + - `#include <string>` |
| 114 | + - std namespace |
| 115 | + - contiguous in memory |
| 116 | + - dynamic size |
| 117 | + - work with input and output streams |
| 118 | + - lots of useful member functions |
| 119 | + - our familiar operators can be used (`+`, `=`, `<`, `<=`, `>`, `>=`, `+=`, `==`, `!=`, `[]`, ...) |
| 120 | + - can be easily converted to C-style Strings if needed |
| 121 | + - safer |
| 122 | + |
| 123 | +### Declaring and initializing |
| 124 | + |
| 125 | +```c++ |
| 126 | +#include <string> |
| 127 | +using namespace std; |
| 128 | + |
| 129 | +string s1; // empty |
| 130 | +// automatically empty - no need to worry about garbage data and initialization |
| 131 | +string s2 {"Frank"}; // Frank |
| 132 | +string s3 {s2}; // Frank: different memories as s2 |
| 133 | +string s4 {"Frank", 3}; // Fra |
| 134 | +string s5 {s3, 0, 2}; // Fr: string s3 and starting at index 0 and copy length of 2 |
| 135 | +string s6 (3, 'X'); // XXX |
| 136 | +``` |
| 137 | +
|
| 138 | +### Assignment |
| 139 | +
|
| 140 | +```c++ |
| 141 | +#include <string> |
| 142 | +using namespace std; |
| 143 | +
|
| 144 | +string s1; |
| 145 | +s1 = "C++ Rocks!"; |
| 146 | +
|
| 147 | +string s2 {"Hello"}; |
| 148 | +s2 = s1; |
| 149 | +``` |
| 150 | + |
| 151 | +### Concatenation |
| 152 | + |
| 153 | +```c++ |
| 154 | +#include <string> |
| 155 | +using namespace std; |
| 156 | + |
| 157 | +string part1 {"C++"}; |
| 158 | +string part2 {"is a powerful"}; |
| 159 | + |
| 160 | +string sentence; |
| 161 | + |
| 162 | +sentence = part1 + " " + part2 + " language"; |
| 163 | +// Output: C++ is a powerful language |
| 164 | + |
| 165 | +// Below is ILLEGAL!!! |
| 166 | +sentence = "C++" + " is powerful"; |
| 167 | +``` |
| 168 | +
|
| 169 | +### Accessing charaacters [] and at() method |
| 170 | +
|
| 171 | +```c++ |
| 172 | +#include <string> |
| 173 | +using namespace std; |
| 174 | +
|
| 175 | +string s1; |
| 176 | +string s2 {"Frank"}; |
| 177 | +
|
| 178 | +cout << s2[0] << endl; // Output: F |
| 179 | +cout << s2.at(0) << endl; // Output: F |
| 180 | +
|
| 181 | +s2[0] = 'f'; // frank |
| 182 | +s2.at(0) = 'p' // prank |
| 183 | +
|
| 184 | +// ========= For loop to access each character ========= |
| 185 | +string s1 {"Frank"}; |
| 186 | +
|
| 187 | +for (char c : s1) { |
| 188 | + cout << c << endl; |
| 189 | +} |
| 190 | +
|
| 191 | +// Output: |
| 192 | +// F |
| 193 | +// r |
| 194 | +// a |
| 195 | +// n |
| 196 | +// k |
| 197 | +// |
| 198 | +
|
| 199 | +// ========= For loop to access each character as integer ========= |
| 200 | +string s1 {"Frank"}; |
| 201 | +
|
| 202 | +for (int c : s1) { |
| 203 | + cout << c << endl; |
| 204 | +} |
| 205 | +
|
| 206 | +// Output: ascii code of the characters |
| 207 | +// 70 // F |
| 208 | +// 114 // r |
| 209 | +// 97 // a |
| 210 | +// 110 // n |
| 211 | +// 107 // k |
| 212 | +// 0 // null character |
| 213 | +``` |
| 214 | + |
| 215 | +### Comparing |
| 216 | + |
| 217 | +`==`, `!=`, `>`, `>=`, `<`, `<=` |
| 218 | + |
| 219 | +The objects are compared character by character lexically. |
| 220 | + |
| 221 | +Can compare: |
| 222 | +- two `std::string` objects |
| 223 | +- `std::string`object and C-style string literal |
| 224 | +- `std::string` object and C-style string variable |
| 225 | + |
| 226 | +Example |
| 227 | + |
| 228 | +```c++ |
| 229 | +string s1{"Apple"}; |
| 230 | +string s2{"Banana"}; |
| 231 | +string s3{"Kiwi"}; |
| 232 | +string s4{"apple"}; |
| 233 | +string s5{s1}; // Apple |
| 234 | + |
| 235 | +s1 == s5; // True |
| 236 | +s1 == s2; // False |
| 237 | +s1 != s2; // True |
| 238 | +s1 < s2; // True |
| 239 | +s2 > s1; // True |
| 240 | +s4 < s5; // False |
| 241 | +s1 == "Apple"; // True |
| 242 | +``` |
| 243 | +
|
| 244 | +### Substrings - `substr` |
| 245 | +
|
| 246 | +Extracts a substring from a `std::string` |
| 247 | +
|
| 248 | +`object.substr(start_index, length)` |
| 249 | +
|
| 250 | +```c++ |
| 251 | +string s1 {"This is a test"}; |
| 252 | +
|
| 253 | +cout << s1.substr(0, 4); // Output: This |
| 254 | +cout << s1.substr(5, 2); // Output: is |
| 255 | +cout << s1.substr(10, 4); // Output: test |
| 256 | +``` |
| 257 | + |
| 258 | +### Searching - `find` |
| 259 | + |
| 260 | +Returns the index of a substring in a `std::string` |
| 261 | + |
| 262 | +`obect.find(search_string)` |
| 263 | + |
| 264 | +```c++ |
| 265 | +string s1 {"This is a test"}; |
| 266 | + |
| 267 | +cout << s1.find("This"); // Output: 0 |
| 268 | +cout << s1.find("is"); // Output: 2 |
| 269 | +cout << s1.find("test"); // Output: 10 |
| 270 | +cout << s1.find("e"); // Output: 11 |
| 271 | +cout << s1.find("is", 4); // Output: 5 |
| 272 | +// start find at specified index, in this case, index 4 |
| 273 | +cout << s1.find("XX"); // Output: string::npos |
| 274 | +// no position available |
| 275 | +``` |
| 276 | +
|
| 277 | +### Removing characters - `erase()` and `cleaer()` |
| 278 | +
|
| 279 | +Removes a substring of characters from a `std::string` |
| 280 | +
|
| 281 | +`object.erase(start_index, length)` |
| 282 | +
|
| 283 | +```c++ |
| 284 | +string s1 {"This is a test"}; |
| 285 | +
|
| 286 | +cout << s1.erase(0, 5); // Output: is a test |
| 287 | +cout << s1.erase(5, 4); // Output: is a |
| 288 | +s1.clear(); // empties string s1 |
| 289 | +``` |
| 290 | + |
| 291 | +### Other useful methods |
| 292 | + |
| 293 | +```c++ |
| 294 | +string s1 {"Frank"}; |
| 295 | +cout << s1.length() << endl; // Output: 5 |
| 296 | + |
| 297 | +s1 += " James"; |
| 298 | +cout << s1 << endl; // Output: Frank James |
| 299 | +``` |
| 300 | +
|
| 301 | +many more... |
| 302 | +
|
| 303 | +### Input with C++ Strings |
| 304 | +
|
| 305 | +Inut `>>` and `getline()` |
| 306 | +
|
| 307 | +Reading `std::string` from `cin` |
| 308 | +
|
| 309 | +```c++ |
| 310 | +string s1; |
| 311 | +
|
| 312 | +cin >> s1; |
| 313 | +// Input: "Hello there" |
| 314 | +
|
| 315 | +cout << s1 << endl; |
| 316 | +// Output: "Hello" |
| 317 | +
|
| 318 | +// Input only accepts up to the first <SPACE> |
| 319 | +
|
| 320 | +// Read entire line until \n |
| 321 | +getline(cin, s1); |
| 322 | +
|
| 323 | +cout << s1 << endl; |
| 324 | +// Output: "Hello there" |
| 325 | +
|
| 326 | +getline(cin, s1, 'x'); |
| 327 | +// Input: this isx |
| 328 | +
|
| 329 | +cout << s1 << endl; |
| 330 | +// Output: "this is" |
| 331 | +``` |
0 commit comments