/** 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) {
    /* First we must create an array to accomodate the integers.  To do 
       this, we must count the number of integers in our string.
       In the following code, we are counting the occurence of commas
       which delimit(separate) the integers and add 1.  We walk every
       character in the string to accomplish this.
    */
    int count= 0;
    for (int i = 0; i < s.length() ; i++) {
      if (s.charAt(i) == ',') count++;
    }
    /* Note that we need to consider when zero or one integers are passed 
     * we do this by checking that the length of our list is longer than 0.
     * This also adds one to the count to accomodate the last integer that
     * is not followed by a comma.
     */    
    if (s.length() > 0) count++;
    /* remember that an array like any object does not exist until it is 
     * instantiated using the "new" command */
    int[] intArray = new int[count];
    int i = 0;
    /* Here we pull apart the string into the integer components.  Note 
     * that we are using the two forms of the substring command in conjunction
     * with the indexOf() command.  This is somewhat different than walking 
     * each character of the string.
     */
    while (s.indexOf(',') >=0) {
      intArray[i] = Integer.parseInt(s.substring(0, s.indexOf(',')));
      s = s.substring(s.indexOf(',') + 2);
      i++;
    } 
    if (s.length() > 0) {
      intArray[i] = Integer.parseInt(s);  // Here we use Integer.parseInt to
    }                                     // convert a string to an integer.
    return intArray;
  }
  
  
}

