The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Our Own Version of Find

If we are looking for a letter in an pstring, we may not want to start at the beginning of the string. One way to generalize the find function is to write a version that takes an additional parameter---the index where we should start looking. Here is an implementation of this function.

int find (pstring s, char c, int i)
{
  while (i<s.length()) {
    if (s[i] == c) return i;
    i = i + 1;
  }
  return -1;
}

Instead of invoking this function on an pstring, like the first version of find, we have to pass the pstring as the first argument. The other arguments are the character we are looking for and the index where we should start.


Last Update: 2005-12-05