/* Title: FTS_Quiz.java ** For: CMPS 134 ** Author: P.M.J., R.W.M. ** ** This Java application contains several methods that are for the purpose of ** modifying and retrieving information from a Field-Terminated String (FTS). ** An FTS is a character string that, typically, contains several related ** pieces of information, each referred to as a "field". So that the beginning ** and ending of each field can be found unambiguously, a "terminator" character ** (or string) appears immediately after each field. For the purposes of this ** program, it is assumed that whatever character is assigned to the DELIMITER ** constant (i.e., final variable) plays the role of the terminator. ** ** As an example, the string ** ** "R01234567;Brown;Charlie;charlie.brown@scranton.edu;" ** (in which the semicolon plays the role of the terminator) is an FTS ** containing four fields, namely "R01234567", "Brown", "Charlie", ** and "charlie.brown@scranton.edu". */ public class FTS_Quiz { private static final char DELIMITER = ';'; /* The main() method is for the purpose of testing the reverseOf1() ** and reverseOf2() methods, which are solutions to the problem posed ** in the quiz. */ public static void main(String[] args) { String fts = args[0]; // args[0] is the command line argument System.out.println("reverseOf1() being applied to " + quoted(fts)); String fts_reverse1 = reverseOf1(fts); System.out.println("\nreverseOf1() maps " + quoted(fts) + " to " + quoted(fts_reverse1)); System.out.println("\n\nreverseOf1() being applied to " + quoted(fts)); String fts_reverse2 = reverseOf2(fts); System.out.println("\nreverseOf2() maps " + quoted(fts) + " to " + quoted(fts_reverse2)); } /* Returns the given string augmented with a leading and trailing ** double quote character. */ public static String quoted(String s) { return "\"" + s + "\""; } // What remain are the methods of interest. // ---------------------------------------- /* Given an FTS, returns the number of fields in it. */ public static int numberOfFields(String fts) { int result = 0; for(int index = 0; index != fts.length(); index++) { if(fts.charAt(index) == DELIMITER) { result = result + 1; } } return result; } /* Given an FTS and an integer i, returns the character position, within ** the FTS, at which its i-th field begins. Fields are numbered starting ** at zero and going left to right. ** Example: In the FTS "R01234567,Brown,Charlie,charlie.brown@scranton.edu", ** fields #0, #1, #2, and #3 begin, respectively, at positions 0, 10, 16, ** and 24. ** ** Pre-condition: 0 <= i < numberOfFields(fts) */ private static int indexOfField(String fts, int i) { int start = 0;; for(int fieldNumber=0; fieldNumber