/* 12/4/2019
 * practice using methods of String class to solve 
 * problems
 * including some String problems from the sample finals
 */
//import java.util.Scanner;

public class StringExercises {
  
  public static void main(String[] args) {
    
    // given 2 Strings, text and a pattern, print whether the pattern
    // occurs in the text twice consecutively (i.e. in tandem)
    int loc;
    String dnaSequ = "ccggttgaggtagtccgccgctagctcattctagcccaggcagaagtctatactttaacccggtcgctgggggtcagacaaaggttaacgtgccttcccctttttatattcctctctagcatg";
    String motif = "ccg";
    loc = dnaSequ.indexOf(motif+motif);
    System.out.println("searched for: " +  motif +
                       " as a tandem, found at: " + loc);
    // another solution (instead of line 16)
    // prints all occurrences of pattern+pattern
    String text = dnaSequ;
    String pattern = motif;
    loc = text.indexOf(pattern);
    while (loc!=-1) {
          int loc2 = text.indexOf(pattern, loc+pattern.length());
          if (loc2 == loc+pattern.length())
             System.out.println("tandem of " + pattern + " found!");
          loc = loc2;
    }
    // Given a string (with spaces) print the nth word
    String msg = "Have a nice day.";
    String[] words = msg.split(" ");
    int n=1;
    if (n < words.length)
      System.out.println(words[n]);
    
    // alternative method (without split)
    // find where the nth word is
    loc=0;
    // repeatedly search for space, n times
    for (int i=0; i<n; i++ ) {
      loc = msg.indexOf(' ', loc+1);
    }    
    // fall out of the loop, loc is pointing to 
    //  the space right before the nth word in the string
    String wordN = msg.substring(loc+1, msg.indexOf(' ', loc+1));
    System.out.println("word " + n + " word is: " + wordN);
     
    // Exercise: Count how many times String pattern
    // occurs in String text
    text = "finding substrings within stringsin";
    pattern = "find";
    int count=0;
    loc = text.indexOf(pattern); // this will find the first char ch in sentence
    while(loc!=-1) {
      count++;
      // find NEXT location
      // System.out.println(pattern + " found at loc " + loc);
      loc = text.indexOf(pattern, loc+1);
    }
    System.out.println(pattern + " found in text " 
                         + " " + count + " times.");
    
// Question 3 Part B: Final 2017
// An int variable n contains a positive integer 
// and a String variable s contains a String. Write 
// the Java code needed to print "YES" if the first 
// n characters of s appear more than once in s, and print "NO" otherwise.
   n=7;
   String s = "Hello how are you. He is leaving.";
   pattern = s.substring(0,n);
   if (s.indexOf(pattern, n) != -1)
      System.out.println("YES");
   else
      System.out.println("NO");
/*** 
  * Final 2017 Question 3 Part A
   A String variable sentence consists of words separated by single spaces.
Write the Java code needed to print the number of words that start with an upper case letter. So for "The deed is done", your code would print 1 but for "The name is Bond, JAMES Bond", your code would print 4. 
****/
   // Given a string (with spaces) print the nth word
    String[] strArr = sentence.split(" ");
    count = 0;
    for (int i=0; i<strArr.length; i++) {
      if (Character.isUpperCase(strArr[i].charAt(0)))
         count++;
    
    // alternative method (without split)
    loc=0;
    count=0;
    // repeatedly search for space
    while (loc!=-1) {
      loc = msg.indexOf(' ', loc+1);
      if (Character.isUpperCase(sentence.charAt(loc+1)))
         count++;
    }    
  }
}
  /*Lab: Read in a name that includes either:
   * First Last
   * First Middle Last
   * Print the initials. If there is a middle name,
   * you must print the middle intial.
   * When you print the initials, even if the user
   * did not type them as capital, you should print
   * them as capital.
   */
  
  
  
  
  
  
  
  