// CharArray.java // Provides methods for manipulating an array of characters. public class CharArray { public static final int MAXLENGTH = 2048; public char[] chars = new char[MAXLENGTH]; public int length; public CharArray(String string) { // sets length to the length of the incoming string // sets each character in the string to the corresponding location in the // array and pads the remaining slots in the array with spaces length = string.length(); for(int i = 0; i < length; i++) chars[i] = string.charAt(i); for(int i = length; i < MAXLENGTH; i++) chars[i] = ' '; } public CharArray(CharArray ch) { // works the same as the above constructor but has a CharArray as its // parameter length = ch.length; for(int i = 0; i < length; i++) chars[i] = ch.chars[i]; for(int i = length; i < MAXLENGTH; i++) chars[i] = ' '; } public int pos(char ch) { // sequential search for a character starting at the beginning of the array boolean done = false; int i = 0; int pos = -1; while (!(done) && (i < length)) if (chars[i] == ch) { pos = i; done = true; } else i += 1; return pos; } public int pos(char ch, int i) { // sequential search for a character starting at position i boolean done = false; int pos = -1; while (!(done) && (i < length)) if (chars[i] == ch) { pos = i; done = true; } else i += 1; return pos; } public char charAt(int pos) { return chars[pos]; } public int pos(char[] ch) { // finds the position of an array within the array of characters boolean done = false; boolean found = false; int i = 0; int pos = -1; int j = 0; while ((!(done)) && (j < ch.length)) { if (chars[i] == ch[j]) { if (j == 0) pos = i; j += 1; } else { if (j != 0) i -= 1; j = 0; } i += 1; if (j == ch.length) found = true; if (i == length) done = true; } if (found) return pos; else return -1; } public int posFromEnd(char ch, int pos) { // finds the position of a character starting at the end of the array boolean done = false; while (!(done) && (pos > -1)) if (chars[pos] == ch) done = true; else pos -= 1; if (done) return pos; else return -1; } public void addOn(char ch) { // inserts a character at the end of the array chars[length] = ch; length += 1; } public void insert(char ch, int pos) { // inserts a character at position pos for(int i = length - 1; i >= pos; i--) chars[i + 1] = chars[i]; chars[pos] = ch; length += 1; } public void delete(int pos, int howMany) { // deletes howMany characters starting at position pos if (pos + 1 != length) { for(int i = pos; i < length; i++) chars[i] = chars[i + howMany]; } length -= howMany; } public String toString() { return new String(chars, 0, length); } }