public class MiniString { private char[] chars; public MiniString(char[] theChars) { chars = new char[theChars.length]; //have to copy one at a time to avoid the aliasing issues for (int i=0; i < theChars.length; i++) { chars[i] = theChars[i]; } } public MiniString(String word) { chars = new char[theChars.length]; for (int i=0; i < word.length(); i++) { chars[i] = word.charAt(i); } } public int length() { return this.chars.length; } public char charAt(int i) { return this.chars[i]; } public String toString() { String builder = ""; for (int i=0; i < chars.length; i++) { builder = builder + chars[i]; } return builder; } //this method duplicates the char array in a way to avoid aliasing public char[] toCharArray() { char[] duplicate = new char[chars.length]; for (int i=0; i < duplicate.length; i++) { duplicate[i] = chars[i]; } return duplicate; } public boolean equals(MiniString other) { if (other == null) { return false; } if (this.chars.length != other.chars.length) { return false; } for (int i=0; i < this.chars.length; i++) { if (this.chars[i] != other.chars[i]) { return false; } } return true; } public int compareTo(MiniString other) { for (int i=0; i < other.chars.length && i < this.chars.length; i++) { if (other.chars[i] < this.chars[i]) { return -1; } if (other.chars[i] > this.chars[i] ) { return 1; } } if (others.length() < this.length()) { return -1; } if (this.length() < others.length()) { return 1; } return 0; } public MiniString concatenate(MiniString addition) { char[] newLetters = new char[this.chars.length + addition.length()]; for (int i=0; i < this.chars.length; i++) { newLetters[i] = this.chars[i]; } for (int i=0; i < addition.length(); i++) { newLetters[i + this.chars.length] = addition.charAt(i); } return new MiniString(newLetters); } public MiniString substring(int start, int end) { char[] newLetters = new char[end - start]; for (int i=0; i < newLetters.length; i++) { newLetters[i] = this.chars[i + start]; } return new MiniString(newLetters); } public MiniString substring(int start) { return this.substring(start, this.chars.length); } public MiniString(char oldChar, char newChar) { char[] replaced = new char[chars.length]; for (int i=0; i < replaced.length; i++) { if (this.chars[i] == oldChar) { replaced[i] = newChar; } else { replaced[i] = chars[i]; } } return new MiniString(replaced); } public int indexOf(char c, int start) { for (int i=start; i < chars.length; i++) { if (this.chars[i] == c) { return i; } } return -1; } public int indexOf(char c) { return indexOf(c, 0); } }