/** LoopStuff contains several examples of loops
 */
import java.io.*;
public class LoopStuff {
  // a single BufferedReader for the whole class
  private static BufferedReader br;

  /** countChar: return the number of occurrences of character
   *  c in String s.
   */
  public static int countChar(String s, char c){
    int k=0;
    int charCount=0;
    // {charCount contains the number of occurrences of char c in
    //            s[0..k-1], where 0<= k <= s.length() }
    while (k != s.length()) {
      if (s.charAt(k) == c) { charCount= charCount + 1; }
      k= k+1;
    }
    // {charCount contains the number of occurrences of char c in
    //            s[0..s.length()-1]}
    return charCount;
  }

  /** printLengths: print the lengths of lines entered at
   *  the keyboard until the user enters "quit"
   */
  public static void printLengths() throws IOException {
    System.out.println("Type lines. End with \"quit\" on a separate line.");
    String input= br.readLine();
    // {lengths of lines typed before input have been printed}
    while ( !input.equals("quit")) {
      System.out.println(input.length());
      input= br.readLine();
    }
    // {lengths of lines typed before input have been printed}
  }

  /** reverse: return original in reverse order.
   */
  public static String reverse(String original) {
    String reversed= "";

    // {reversed= original[0..k-1] in reverse order}
    for (int k = 0; k != original.length(); k++) {
      reversed= original.substring(k,k+1) + reversed;
    }
    // {reversed= original[0..length()-1 in reverse order}
    return reversed;
  }

  /** listDivisorsUpTo: list the divisors of each integer from
   *  1..n-1
   */
  public static void listDivisorsUpTo(int n) {
    // {divisors of integers from 1..k-1 listed}
    for (int i= 1; i != n; i++) {
      System.out.println(listDivisors(i));
    }
    //{ {divisors of integers from 1..n-1 listed}
  }

  /** listDivisors: return the positive divisors of i as a
   *  String.  For example, if i= 12 return
   *  i: 1, 2, 3, 4, 6, 12
   */
  public static String listDivisors(int i) {
    String divList= "" + i + ": "; // start the list off

    // {divList contains divisors of i from 1..k-1}
    for (int k= 1; k != i+1; k++) {
      if (i%k == 0) { divList += "" + k + ", "; }
    }
    // {divList contains divisors of i from 1..i}
    return divList;
  }


  /** main: try out the methods above
   */
  public static void main(String[] args) throws IOException {
    // initialize the BufferedReader
    br= new BufferedReader(new InputStreamReader(System.in));

    // Try out countChar
    System.out.println("Type a single character to search for");
    char c= br.readLine().charAt(0);
    System.out.println("Type a String to search for the character in:");
    String s= br.readLine();
    System.out.println("There are: " + countChar(s, c) +
		       " occurrences of " + c + " in " + s);

    // try out reverse
    System.out.println("\nType a string to reverse:");
    s= br.readLine();
    System.out.println("" + s + " reversed is: " +
		       reverse(s));

    // try out listDivisorsUpTo
    System.out.println("\nType a positive integer:");
    int n= Integer.parseInt(br.readLine());
    System.out.println("Divisors from 1.." + n +":");
    listDivisorsUpTo(n+1);
    System.out.println("");
    printLengths();
  }


}

