7.5. Traversal

A common thing to do with a string is start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal. A natural way to encode a traversal is with a while statement.

The active code below outputs each letter of string fruit using a while loop.

Accessing a string character
 1#include <cstddef>
 2#include <iostream>
 3#include <string>
 4
 5using std::size_t;
 6
 7int main() {
 8    std::size_t index = 0;
 9    std::string fruit = "apple";
10    while (index < fruit.size()) {
11        char letter = fruit[index];
12        std::cout << letter << '\n';
13        index = index + 1;
14    }
15}

This loop traverses the string and outputs each letter on a line by itself. Notice that the condition is index < fruit.size(), which means that when index is equal to the length of the string, the condition is false and the body of the loop is not executed. The last character we access is the one with the index fruit.size()-1.

The name of the loop variable is index. An index is a variable or value used to specify one member of an ordered set, in this case the set of characters in the string. The index indicates (hence the name) which one you want. The set has to be ordered so that each letter has an index and each index refers to a single character.

As an exercise, write a function that takes a string as an argument and that outputs the letters backwards, all on one line.

Try writing the reverse_word function in the commented section of the active code below. If done correctly, the program outputs "olleh". If you get stuck, you can reveal the extra problem at the end for help.

Example traversal_AC_2
 1#include <cstddef>
 2#include <iostream>
 3#include <string>
 4using std::size_t;
 5
 6void reverse_word (std::string word) {
 7    // ``reverse_word`` should take the letters of ``word``
 8    // and output them in reverse.
 9}
10
11int main() {
12    reverse_word("hello");
13}
Reveal Problem

Let's write the code for the reverse_word function. reverse_word should take a string as a parameter and output the letters backwards.

  1.     x = x + 1;
        std::cout << input[x];
      }
    } #distractor
  2.     x = x - 1;
        std::cout << input[x];
      }
    }
  3. std::size_t x = input.size() - 1;
  4. std::size_t x = input.size();
  5. void reverse_word (string input) {
  6. while (x > 0) {
  7. while (x >= 0) {

Start at the size and subtract one before accessing a character. This also handles an empty string: the loop never starts. An unsigned index cannot become negative, so x >= 0 cannot terminate this loop.

Q1

How many times is the letter o printed by the following statements?

string s = "coding rocks";
size_t i = 1;
while (i < s.length()) {
  cout << s[i] << '\n';
  i = i + 2;
}

Q2

What is printed when the code is run?

string truth = "engr101";
size_t index = 0;
int counter = 0;
while (index < truth.length()) {
  cout << truth[index] << ' ';
  index = index + counter;
  counter = counter + 1;
}