CS 1713 Introduction to Computer Science: Spring 2007


Problem from class on 4/13/07:


Write a method called findSecond that takes two parameters, a String and a char.
It returns the position of the second occurrence of the second parameter in the first parameter,
or -1 if the character does not appear at least twice..
For example:
    findSecond("abcdabcabc",'a') returns 4
    findSecond("abcdabcabc",'d') returns -1

public int findSecond(String s, char c) {
   int start;
   start = s.indexOf(c);
   if (start == -1)
      return -1;
   for (int i=start+1; i<s.length(); i++)
      if (s.charAt(i) == c)
          return i;
   return -1;
}


Problem for Wednesday, April 18:
Draw a diagram (like exam 2, problems 6 and 7).