.. _strings-things-extracting-characters-from-a-string: Extracting characters from a string ----------------------------------- .. index:: pair: string; string extraction Strings are called "strings" because they are made up of a sequence, or string, of characters. Imagine a string of beads, where each character is a bead in the string. .. graphviz:: :name: fig_string_characters :caption: Characters in a string :alt: a sequence of character cells representing a string :align: center graph char_array { fontname = "Bitstream Vera Sans" node [ fontname = "Bitstream Vera Sans" fontsize = 14 shape = "circle" style=filled fillcolor=lightblue ] a -- b -- c -- d -- e -- f -- h -- i -- j -- k [ constraint=false] } The first operation we are going to perform on a string is to extract one of the characters. C++ uses square brackets (``[`` and ``]``) for this operation. Take a look at the active code below. We extract the character at index 1 from string ``fruit`` using ``[`` and ``]``. .. tb-code:: cpp :name: extracting_characters_AC_1 :caption: Accessing a string character #include #include int main() { std::string fruit = "orange"; char letter = fruit[1]; std::cout << letter; } The expression ``fruit[1]`` indicates that I want character number 1 from the string named ``fruit``. The result is stored in a ``char`` named ``letter``. When I output the value of ``letter``, I get a surprise: :: r ``r`` is not the first letter of ``"orange"``. Unless you are a computer scientist. Programmers (and most programming languages) always start counting from zero. The 0th letter (“zeroeth”) of ``"orange"`` is ``o``. The 1th letter (“oneth”) is ``r`` and the 2th (“twoeth”) letter is ``a``. .. note:: In C++, indexing begins at 0! If you want the zereoth letter of a string, then you have to put zero in the square brackets. The active code below accesses the first character in string ``fruit``. .. tb-code:: cpp :name: extracting_characters_AC_2 :caption: Accessing a string character #include #include int main() { string fruit = "orange"; char letter = fruit[0]; cout << letter << '\n'; } .. tb-group:: :name: self_check .. tb-tab:: Q1 .. tb-choice:: :name: extracting_characters_1 What would replace the "?" in order to access the letter "b" in the string below? .. code-block:: cpp :linenos: #include using namespace std; int main () { string bake = "bake a cake!"; char letter = bake[?]; } - [ ] 1 Don't forget that computer scientists do not start counting at 1! - [x] 0 Yes, this would access the letter "b". - [ ] 2 This would access the letter "k". .. tb-tab:: Q2 .. tb-choice:: :name: extracting_characters_2 What is printed when the code below is run? .. code-block:: cpp :linenos: #include using namespace std; int main () { string lunch = "hello"; string person = "deejay"; lunch[0] = lunch[3]; cout << lunch; } - [ ] lunch When we cout a string we print its content not its name. - [ ] jello Carefully check which string(s) we are indexing into. - [x] lello Correct! We copy the 'l' from position 3 of "hello" to position 0. - [ ] heljo Consider which string(s) we are indexing into. .. tb-tab:: Q3 .. tb-click:: :name: extracting_characters_3 Click on each spot where a character in a string is accessed. .. code-block:: cpp int main() { string fruit = "apple"; char letter = fruit[2]; cout << fruit << '\n'; cout << fruit[4] << '\n'; } .. tb-miss:: text:int main() { Remember, square brackets [] are used to access a character in a string. .. tb-miss:: text:string fruit = "apple"; Remember, square brackets [] are used to access a character in a string. .. tb-hit:: text:fruit[2]; Correct. .. tb-miss:: text:cout << fruit << '\n'; Remember, square brackets [] are used to access a character in a string. .. tb-hit:: text:fruit[4] Correct. .. tb-tab:: Q4 .. tb-parsons:: :name: extracting_characters_4 Construct a block of code that correctly prints the letter "a". .. code-block:: cpp {{group}} string x; {{endgroup}} {{group}} x = "It is warm outside!"; {{endgroup}} {{distractor}} {{group}} x = "It is warm outside" {{endgroup}} {{group}} cout << x[7] << '\n'; {{endgroup}} {{distractor}} {{group}} cout << x[8] << '\n'; #distractor {{endgroup}}