/* 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