'a').
A string is a sequence of
characters.
The characters in the string are enclosed in quotation marks
(for example, "monkey").
char is
usually an 8-bit type,
which corresponds to one character.
For example, the following program defines a
character variable and prints it:
char x = 'a'; cout << x << "\n"; // aEach character has two characteristics: character representation and character code. The character representation means what the character looks like, and the character code is an integer corresponding to the character.
You can print the character's character code by changing its
type to int.
For example, the code for 'a' is 97,
which can be observed as follows:
cout << (int)'a' << "\n"; // 97Correspondingly, the character corresponding to a character code can be printed by casting to a
char:
cout << (char)97 << "\n"; // aThe character representation is, in fact, just an alternative way to specify an integer. The difference is that a
char-type
prints as a character representation and
int-type prints as a character code.
The following code illustrates this:
int x = 'a'-5; cout << x << "\n"; // 92In the code
'a' stands for 97,
so x will be set to 97-5 = 92.
| 32 | 0 | 48 | @ | 64 | P | 80 | ` | 96 | p | 112 | ||||||
| ! | 33 | 1 | 49 | A | 65 | Q | 81 | a | 97 | q | 113 | |||||
| " | 34 | 2 | 50 | B | 66 | R | 82 | b | 98 | r | 114 | |||||
| # | 35 | 3 | 51 | C | 67 | S | 83 | c | 99 | s | 115 | |||||
| $ | 36 | 4 | 52 | D | 68 | T | 84 | d | 100 | t | 116 | |||||
| % | 37 | 5 | 53 | E | 69 | U | 85 | e | 101 | u | 117 | |||||
| & | 38 | 6 | 54 | F | 70 | V | 86 | f | 102 | v | 118 | |||||
| ' | 39 | 7 | 55 | G | 71 | W | 87 | g | 103 | w | 119 | |||||
| ( | 40 | 8 | 56 | H | 72 | X | 88 | h | 104 | x | 120 | |||||
| ) | 41 | 9 | 57 | I | 73 | Y | 89 | i | 105 | y | 121 | |||||
| * | 42 | : | 58 | J | 74 | Z | 90 | j | 106 | z | 122 | |||||
| + | 43 | ; | 59 | K | 75 | [ | 91 | k | 107 | { | 123 | |||||
| , | 44 | < | 60 | L | 76 | \ | 92 | l | 108 | | | 124 | |||||
| - | 45 | = | 61 | M | 77 | ] | 93 | m | 109 | } | 125 | |||||
| . | 46 | > | 62 | N | 78 | ^ | 94 | n | 110 | ~ | 126 | |||||
| / | 47 | ? | 63 | O | 79 | _ | 95 | o | 111 |
'\n' with a code of 10.
A handy feature in the ASCII character set is, that the numbers 0 through 9 are consecutive, similarly, the letters a-z and A-Z. This allows you to write the following code, for example:
for (char x = 'a'; x <= 'z'; x++) {
cout << x;
}
cout << "\n";
The code prints out the following:
abcdefghijklmnopqrstuvwxyz
string.
The following code generates and prints a string:
string s = "monkey"; cout << s << "\n"; // monkeyWe can also create a string by entering a length and repeat character:
string s(5,'a'); cout << s << "\n"; // aaaaaYou can concatenate strings with the
+ operator
through:
string a = "ex"; string b = "ample"; cout << a+b << "\n"; // exampleThe
size function gives the number of characters:
string s = "monkey"; cout << s.size() << "\n"; // 6
[].
string s = "monkey"; cout << s[0] << "\n"; // m cout << s[1] << "\n"; // oYou can also change the contents of a string:
string s = "monkey"; s[3] = 'l'; cout << s << "\n"; // monleyThe following code goes through the characters in the string and prints their codes:
string s = "monkey";
for (int i = 0; i < s.size(); i++) {
cout << i << " " << s[i] << " " << (int)s[i] << "\n";
}
It prints out the following:
0 m 109 1 o 111 2 n 110 3 k 107 4 e 101 5 y 121
substr(k) function extracts the string
starting from index k.
The substr(k,p) function in turn extracts
p characters starting from index k.
If the string runs out, there will be fewer characters.
string s = "cembalo"; cout << s.substr(2) << "\n"; // mbalo cout << s.substr(2,3) << "\n"; // mba cout << s.substr(2,10) << "\n"; // mbaloThe
find(x) function looks for the first point,
where x begins.
The find(x, k) function in turn searches
for the first such point that is k or greater.
string s = "banana";
cout << s.find("ana") << "\n"; // 1
cout << s.find("na") << "\n"; // 2
cout << s.find("na",3) << "\n"; // 4
If the searched item is not present, the function returns a value
npos corresponding to -1.
string s = "banana";
if (s.find("mo") == -1) {
// not found
}
to_string converts a number to a string:
int x = 123; string s = to_string(x);There are also functions for converting a string to a number. For example, the functions
stoi and stod
convert a string to an int and double
number, respectively.
string s = "123"; int a = stoi(s); double b = stod(s);
The difficulty is that are there are many different ways to internally represent these characters, and the precise representation is often system dependent. Nowadays, the common way is UTF-8, where non-ASCII characters are represented internally as multiple characters. The following code illustrates this:
string s = "pöllö";
cout << s << "\n";
cout << s.size() << "\n";
for (int i = 0; i < s.size(); i++) {
cout << i << " " << s[i] << " " << (int)s[i] << "\n";
}
Because ö is not in the ASCII character set,
it is stored as two characters,
so the string length is 7 instead of 5.
If you try to print the characters, instead of ö there will appear
two special characters that cannot be printed.
pöllö 7 0 p 112 1 � -61 2 � -74 3 l 108 4 l 108 5 � -61 6 � -74