Tie koodariksi

C++ Programming

Chapter 10: Characters and Strings

Terminology

A character is a single symbol, such as a number, letter, or punctuation. The character is enclosed in apostrophes (for example, 'a').

A string is a sequence of characters. The characters in the string are enclosed in quotation marks (for example, "monkey").

Characters and codes

In C++, the data type 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"; // a
Each 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"; // 97
Correspondingly, the character corresponding to a character code can be printed by casting to a char:
cout << (char)97 << "\n"; // a
The 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"; // 92
In the code 'a' stands for 97, so x will be set to 97-5 = 92.

Character sets

A character set determines which characters are available and what their character codes are. Character sets vary from system to system, but is usually based on the ASCII character set, whose usual characters are:
32048@64P80`96p112
!33149A65Q81a97q113
"34250B66R82b98r114
#35351C67S83c99s115
$36452D68T84d100t116
%37553E69U85e101u117
&38654F70V86f102v118
'39755G71W87g103w119
(40856H72X88h104x120
)41957I73Y89i105y121
*42:58J74Z90j106z122
+43;59K75[91k107{123
,44<60L76\92l108|124
-45=61M77]93m109}125
.46>62N78^94n110~126
/47?63O79_95o111
Characters before code 32 are special characters, such as a newline '\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

Strings

The C++ string type is string. The following code generates and prints a string:
string s = "monkey";
cout << s << "\n"; // monkey
We can also create a string by entering a length and repeat character:
string s(5,'a');
cout << s << "\n"; // aaaaa
You can concatenate strings with the + operator through:
string a = "ex";
string b = "ample";
cout << a+b << "\n"; // example
The size function gives the number of characters:
string s = "monkey";
cout << s.size() << "\n"; // 6

Strings as arrays

A string can be thought of as an array whose elements are characters. As in an array, the characters are numbered from zero and are referred to via [].

string s = "monkey";
cout << s[0] << "\n"; // m
cout << s[1] << "\n"; // o
You can also change the contents of a string:
string s = "monkey";
s[3] = 'l';
cout << s << "\n"; // monley
The 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

Other functions

The 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"; // mbalo
The 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
}

Strings and numbers

The function 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);

What about umlauts?

The ASCII character set shown above does not contain any umlauts (ä, ö, etc.) and many other symbols. But what if you want to use these characters in your program?

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

Datatähti 2027 – peruskoulun ja lukion ohjelmointikilpailu käynnissä 26.10.–8.11.2026