/** A class to demonstrate String Methods */
public class StringMethods {
  /** Reverses the order of the letters in a string.
   * @param s the String to be reversed.
   * @return the reversed String.
   */
  public static String reverse(String s) {
    String result = "";
    for (int i = s.length() -1; i >=0; i--) {
      result += s.charAt(i);
    }
    return result;
  }
  
  /** Counts the occurences of a specified character in a string.  Uses 
   *  the string method indexOf() to locate the character.
   * @param s the String to be searched for the character.
   * @param c the character to be searched for in the String.
   * @return the number of occurences of char c in String s as an int.
   */
  public static int countChar(String s, char c) {
    int count = 0;
    // Have decided that this might be better in a while loop.
    // Have rewritten the code accordingly. 
    // The old code using a for loop is here - but has been commented it out.
    /*for (int i = s.indexOf(c); i >= 0 && i < s.length(); i++) {
      i = s.indexOf(c, i);
      if (i== -1) { 
        i--; 
      }else {
        count ++;
      }
    }
    */
    int i = 0;
    while (i > -1 && i < s.length()) {
      i = s.indexOf(c,i);
      if (i > -1) {
        count++;
        i++;
      }
    }
    return count;
  }
  
  /** Counts the occurences of a specified character in a string. Uses
   *  the String method charAt() and walks each character in the string.
   * @param s the String to be searched for the character.
   * @param c the character to be searched for in the String.
   * @return the number of occurences of char c in String s as an int.
   */
   public static int countChar2(String s, char c) {
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
      if (c == s.charAt(i)) count++;
    }
    return count;
   }
   
   /** Take apart a string of integers that are separated by ", " and 
    *  return an array of integers. 
    *  @param s the String to be converted to integers.
    *  @return the array of integers.
    * */
   // to be completed in class on Tuesday.
   public static int[] stringToInts(String s) {
   
     return new int[0];
   }
   
   
 
}

    