/** the StringSearcing class 
 compareTo, indexOf
 */
import java.util.Scanner;

public class StringSearching {
  public static void main(String[] args) {
    String name="John Doe";
    String name1 = "abcdooooa;jdfklsjdoooa;o o a;jfldj o o o o o o ooooozzz";// "John Doe";
    System.out.println("name1 is " +  name1);
    String name2 = "Alice";
    System.out.println("name2 is " +  name2);
    String name3= "John Q. Adams";
    System.out.println("name3 is " +  name3);
    if (name1.compareTo(name3)<0)
      System.out.println(name1 + " should come before " + name3);
    // print initials of name (some pple have 2 initial and some have 3)
    System.out.println("First initial is: " + name1.charAt(0));
    int loc = name1.indexOf(' ');
    System.out.println("loc of space is " + loc +
                       ", last name initial is: " + name1.charAt(loc+1));
    // check if there is a middle initial -- you can count # of spaces    
    
    
    System.out.println("About to execute LOOP: ");
    int countos = 0;
    loc = name1.indexOf('o', 0); 
    while (loc != -1)  {
         countos++;
         System.out.print(loc + ", ");
         loc = name1.indexOf('o', loc+1); 
    }
    System.out.println("\nWe found " + countos + " little o's in the string " +
                       name1);
    // now we will do the same thing using a for loop
    countos=0;
    for (int i=0; i<name1.length(); i++) 
       if (name1.charAt(i)=='o')
           countos++;
    System.out.println("using for loop, countos is: " + countos);
    String title = "Finding Substrings in Strings";
    String repeat = "in";
    int countrepeats = 0;
    loc = title.indexOf(repeat, 0); 
    while (loc != -1)  {
         countrepeats++;
         System.out.print(loc + ", ");
         loc = title.indexOf(repeat, loc+1); 
    }
    System.out.println("\nWe found " + countrepeats + " occurrences of " +
                       repeat + " in the string " + title);
                      
    String zip = "11210";
    // Strings are IMMUTABLE which means unchangeable
    if (zip.startsWith("112"))
      System.out.println("your zipcode is in Brooklyn.");
    
    String sub = "ring";
    int beg = title.indexOf(sub);
    if (beg!=-1) // if it is found
        System.out.println(title.substring(beg, title.length()));
   }
}