Return to Snippet

Revision: 33790
at November 19, 2010 20:25 by trusktr


Updated Code
import java.util.ArrayList;

public class MyString{
	
	// ########## Data Objects ########## (
		private char[] theString, result;
//  )
	// ########## Constructors ########## (
		public MyString(){ // default
			theString = new char[] {'x'};
			result = new char[] {'x'};
		}
		public MyString(String s){ // parameterized
			theString = s.toCharArray();
			result = new char[] {'x'};
		}
		public MyString(char[] s){ // parameterized
			int length = s.length;
			theString = new char[length];
			for(int i=0; i<length; i++){
				theString[i] = s[i];
			}
			result = new char[] {'x'};
		}
		public MyString(MyString s){ // copy
			theString = s.contents();
			result = new char[] {'x'};
		}
//	)
	// ########## Accessors ########## (
		private char[] contents(){
			char[] s = theString;
			return s;
		}
		public int length(){
			return theString.length;
		}
		public String result(){
			return new String(result);
		}
		public String toString(){
			return new String(theString);
		}
		public char get(int n){
			return theString[n];
		}
//  )
	// ########## Mutators ########## (
		public MyString left(int n){
			int stringLength = theString.length;
			result = new char[n];
			if(n > stringLength){ 				// If the space is bigger than the string...
				for(int i=0; i<stringLength; i++){
					result[i] = theString[i];
				}
				for(int j=stringLength; j<n; j++){
					result[j] = ' ';
				}
			}
			if(n <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int i=0; i<n; i++){
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString right(int n){
			int stringLength = theString.length;
			int emptySpace = n - stringLength;
			result = new char[n];
			if(n > stringLength){ 				// If the space is bigger than the string...
				for(int j=0; j<emptySpace; j++){
					result[j] = ' ';
				}
				int count = 0;
				for(int i=emptySpace; i<n; i++){
					result[i] = theString[count];
					count++;
				}
			}
			if(n <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<n; j++){
					result[j] = theString[j];
				}
			}
			return new MyString(result);
		}
		public MyString mid(int n){
			int stringLength = theString.length;
			int emptySpace = n - stringLength;
			int leftPadding=0, rightPadding=0;
			result = new char[n];
			if(n > stringLength){ 				// If the space is bigger than the string...
				if ((emptySpace % 2) == 0){ // If the empty space size is even...
					leftPadding = (emptySpace / 2); // The left padding is equal to the right padding.
					rightPadding = leftPadding;
				}
				if ((emptySpace % 2) == 1){ // If the empty space size is odd...
					rightPadding = (emptySpace / 2) + 1; // The left padding is smaller than the right by 1 space.
					leftPadding = emptySpace / 2;
				}
				for(int i=0; i<leftPadding; i++){
					result[i] = ' ';
				}
				int count = 0;
				for(int j=leftPadding; j<(leftPadding + stringLength); j++){
					result[j] = theString[count];
					count++;
				}
				for(int k=(leftPadding + stringLength); k<n; k++){
					result[k] = ' ';
				}
			}
			if(n <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<n; j++){
					result[j] = theString[j];
				}
			}
			return new MyString(result);
		}
		public boolean has(String s){ // Determines whether or not a search term exists in our string (true or false).
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so break...
			}
			return found;
		}
		public int firstOcurrenceOf(String s, int n){ // Returns the position at which the search term is first encountered. The search begins at the desired starting point n.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int start = n; // The location in in our string where we begin the search.
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=start; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength || start >= stringLength) { // If our search term is bigger than our main string itself, or the starting point is outside the string's boundaries...
					break; // Then obviously our search term will not be found, so prevent the CPU from wasting it's resources on the test or encountering errors...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so exit the loop...
			}
			return foundAt;
		}
		public int firstOcurrenceOf(String s){ // This is simply an overloaded version of the previous method so that if you leave off the "n" parameter, it defaults to 0.
			return firstOcurrenceOf(s, 0);
		}
		public int[] findAll(String s){ // Returns an array containing all the locations in our main string where a search term is found to begin.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			ArrayList<Integer> locations = new ArrayList<Integer>(); // Locations where a match is found...
			int foundAt[]; // The final array to output containing the locations of the ArrayList above...
			int charMatches = 0; // Counter for matching characters...
			
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					found = false; // Set to false for each test unless we find a matching term...
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						locations.add(i); // The match is at position i. Add it to our ArrayList.
					}
				}
			}
			int termCount = locations.size();
			foundAt = new int[termCount];
			for (int h=0; h<termCount; h++) {
				foundAt[h] = locations.get(h);
			}
			return foundAt;
		}
		public MyString uppercase(){ // Turns the string to all uppercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 97 && theString[i] <= 122) {
					result[i] = (char)(theString[i] - ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString lowercase(){ // Turns the string to all lowercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 65 && theString[i] <= 90) {
					result[i] = (char)(theString[i] + ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString invert(){ // Inverts the string so it is spelled backwards.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=stringLength-1; i>=0; i--) {
				result[ (stringLength-1)-i ] = theString[i];
			}
			return new MyString(result);
		}
//  )
}

Revision: 33789
at October 13, 2010 11:03 by trusktr


Updated Code
import java.util.ArrayList;

public class MyString{
	
	// ########## Data Objects ########## (
		private char[] theString, result;
//  )
	// ########## Constructors ########## (
		public MyString(){ // default
			theString = new char[] {'x'};
			result = new char[] {'x'};
		}
		public MyString(String s){ // parameterized
			theString = s.toCharArray();
			result = new char[] {'x'};
		}
		public MyString(char[] s){ // parameterized
			int length = s.length;
			theString = new char[length];
			for(int i=0; i<length; i++){
				theString[i] = s[i];
			}
			result = new char[] {'x'};
		}
		public MyString(MyString s){ // copy
			theString = s.contents();
			result = new char[] {'x'};
		}
//	)
	// ########## Accessors ########## (
		private char[] contents(){
			char[] s = theString;
			return s;
		}
		public int length(){
			return theString.length;
		}
		public String result(){
			return new String(result);
		}
		public String toString(){
			return new String(theString);
		}
//  )
	// ########## Mutators ########## (
		public MyString left(int size){
			int stringLength = theString.length;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				for(int i=0; i<stringLength; i++){
					result[i] = theString[i];
				}
				for(int j=stringLength; j<size; j++){
					result[j] = ' ';
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int i=0; i<size; i++){
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString right(int size){
			int stringLength = theString.length;
			int emptySpace = size - stringLength;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				for(int j=0; j<emptySpace; j++){
					result[j] = ' ';
				}
				int count = 0;
				for(int i=emptySpace; i<size; i++){
					result[i] = theString[count];
					count++;
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<size; j++){
					result[j] = theString[j];
				}
			}
			return new MyString(result);
		}
		public MyString mid(int size){
			int stringLength = theString.length;
			int emptySpace = size - stringLength;
			int leftPadding=0, rightPadding=0;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				if ((emptySpace % 2) == 0){ // If the empty space size is even...
					leftPadding = (emptySpace / 2); // The left padding is equal to the right padding.
					rightPadding = leftPadding;
				}
				if ((emptySpace % 2) == 1){ // If the empty space size is odd...
					rightPadding = (emptySpace / 2) + 1; // The left padding is smaller than the right by 1 space.
					leftPadding = emptySpace / 2;
				}
				for(int i=0; i<leftPadding; i++){
					result[i] = ' ';
				}
				int count = 0;
				for(int j=leftPadding; j<(leftPadding + stringLength); j++){
					result[j] = theString[count];
					count++;
				}
				for(int k=(leftPadding + stringLength); k<size; k++){
					result[k] = ' ';
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<size; j++){
					result[j] = theString[j];
				}
			}
			return new MyString(result);
		}
		public boolean hasPattern(String s){ // Determines whether or not a search term exists in our string (true or false).
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so break...
			}
			return found;
		}
		public int firstOcurrenceOf(String s, int n){ // Returns the position at which the search term is first encountered. The search begins at the desired starting point n.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int start = n; // The location in in our string where we begin the search.
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=start; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength || start >= stringLength) { // If our search term is bigger than our main string itself, or the starting point is outside the string's boundaries...
					break; // Then obviously our search term will not be found, so prevent the CPU from wasting it's resources on the test or encountering errors...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so exit the loop...
			}
			return foundAt;
		}
		public int firstOcurrenceOf(String s){ // This is simply an overloaded version of the previous method so that if you leave off the "n" parameter, it defaults to 0.
			return firstOcurrenceOf(s, 0);
		}
		public int[] findAll(String s){ // Returns an array containing all the locations in our main string where a search term is found to begin.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			ArrayList<Integer> locations = new ArrayList<Integer>(); // Locations where a match is found...
			int foundAt[]; // The final array to output containing the locations of the ArrayList above...
			int charMatches = 0; // Counter for matching characters...
			
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					found = false; // Set to false for each test unless we find a matching term...
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						locations.add(i); // The match is at position i. Add it to our ArrayList.
					}
				}
			}
			int termCount = locations.size();
			foundAt = new int[termCount];
			for (int h=0; h<termCount; h++) {
				foundAt[h] = locations.get(h);
			}
			return foundAt;
		}
		public MyString uppercase(){ // Turns the string to all uppercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 97 && theString[i] <= 122) {
					result[i] = (char)(theString[i] - ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString lowercase(){ // Turns the string to all lowercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 65 && theString[i] <= 90) {
					result[i] = (char)(theString[i] + ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString invert(){ // Inverts the string so it is spelled backwards.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=stringLength-1; i>=0; i--) {
				result[ (stringLength-1)-i ] = theString[i];
			}
			return new MyString(result);
		}
//  )
}

Revision: 33788
at October 13, 2010 10:49 by trusktr


Updated Code
import java.util.ArrayList;

public class MyString{
	
	// ########## Data Objects ########## (
		private char[] theString, result;
//  )
	// ########## Constructors ########## (
		public MyString(){ // default
			theString = new char[] {'x'};
			result = new char[] {'x'};
		}
		public MyString(String s){ // parameterized
			theString = s.toCharArray();
			result = new char[] {'x'};
		}
		public MyString(char[] s){ // parameterized
			int length = s.length;
			theString = new char[length];
			for(int i=0; i<length; i++){
				theString[i] = s[i];
			}
			result = new char[] {'x'};
		}
		public MyString(MyString s){ // copy
			theString = s.contents();
			result = new char[] {'x'};
		}
//	)
	// ########## Accessors ########## (
		private char[] contents(){
			char[] s = theString;
			return s;
		}
		public int length(){
			return theString.length;
		}
		public String result(){
			return new String(result);
		}
		public String toString(){
			return new String(theString);
		}
//  )
	// ########## Mutators ########## (
		public MyString left(int size){
			int stringLength = theString.length;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				for(int i=0; i<stringLength; i++){
					result[i] = theString[i];
				}
				for(int j=stringLength; j<size; j++){
					result[j] = ' ';
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int i=0; i<size; i++){
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString right(int size){
			int stringLength = theString.length;
			int emptySpace = size - stringLength;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				for(int j=0; j<emptySpace; j++){
					result[j] = ' ';
				}
				int count = 0;
				for(int i=emptySpace; i<size; i++){
					result[i] = theString[count];
					count++;
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<size; j++){
					result[j] = theString[j];
				}
			}
			return new MyString(result);
		}
		public MyString mid(int size){
			int stringLength = theString.length;
			int emptySpace = size - stringLength;
			int leftPadding=0, rightPadding=0;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				if ((emptySpace % 2) == 0){ // If the empty space size is even...
					leftPadding = (emptySpace / 2); // The left padding is equal to the right padding.
					rightPadding = leftPadding;
				}
				if ((emptySpace % 2) == 1){ // If the empty space size is odd...
					rightPadding = (emptySpace / 2) + 1; // The left padding is smaller than the right by 1 space.
					leftPadding = emptySpace / 2;
				}
				for(int i=0; i<leftPadding; i++){
					result[i] = ' ';
				}
				int count = 0;
				for(int j=leftPadding; j<(leftPadding + stringLength); j++){
					result[j] = theString[count];
					count++;
				}
				for(int k=(leftPadding + stringLength); k<size; k++){
					result[k] = ' ';
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<size; j++){
					result[j] = theString[j];
				}
			}
			return new MyString(result);
		}
		public boolean hasPattern(String s){ // Determines whether or not a search term exists in our string (true or false).
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so break...
			}
			return found;
		}
		public int firstOcurrenceOf(String s, int n){ // Returns the position at which the search term is first encountered. The search begins at the desired starting point n.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int start = n; // The location in in our string where we begin the search.
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=start; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength || start >= stringLength) { // If our search term is bigger than our main string itself, or the starting point is outside the string's boundaries...
					break; // Then obviously our search term will not be found, so prevent the CPU from wasting it's resources on the test or encountering errors...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so exit the loop...
			}
			return foundAt;
		}
		public int firstOcurrenceOf(String s){ // This is simply an overloaded version of the previous method so that if you leave off the "n" parameter, it defaults to 0.
			return firstOcurrenceOf(s, 0);
		}
		public int[] findAll(String s){ // Returns an array containing all the locations in our main string where a search term is found to begin.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			ArrayList<Integer> locations = new ArrayList<Integer>(); // Locations where a match is found...
			int foundAt[]; // The final array to output containing the locations of the ArrayList above...
			int charMatches = 0; // Counter for matching characters...
			
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					found = false; // Set to false for each test unless we find a matching term...
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						locations.add(i); // The match is at position i. Add it to our ArrayList.
					}
				}
			}
			int termCount = locations.size();
			foundAt = new int[termCount];
			for (int h=0; h<termCount; h++) {
				foundAt[h] = locations.get(h);
			}
			return foundAt;
		}
		public MyString uppercase(){ // Turns the string to all uppercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 97 && theString[i] <= 122) {
					result[i] = (char)(theString[i] - ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString lowercase(){ // Turns the string to all lowercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 65 && theString[i] <= 90) {
					result[i] = (char)(theString[i] + ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString invert(){ // Inverts the string so it is spelled backwards.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=stringLength-1; i>=0; i--) {
				result[ (stringLength-1)-i ] = theString[i];
			}
		}
			return new MyString(result);
//  )
}

Revision: 33787
at October 13, 2010 09:50 by trusktr


Updated Code
import java.util.ArrayList;

public class MyString{
	
	// ########## Data Objects ########## (
		private char[] theString, result;
//  )
	// ########## Constructors ########## (
		public MyString(){ // default
			theString = new char[] {'x'};
			result = new char[] {'x'};
		}
		public MyString(String s){ // parameterized
			theString = s.toCharArray();
			result = new char[] {'x'};
		}
		public MyString(char[] s){ // parameterized
			int length = s.length;
			theString = new char[length];
			for(int i=0; i<length; i++){
				theString[i] = s[i];
			}
			result = new char[] {'x'};
		}
		public MyString(MyString s){ // copy
			theString = s.contents();
			result = new char[] {'x'};
		}
//	)
	// ########## Accessors ########## (
		private char[] contents(){
			char[] s = theString;
			return s;
		}
		public int length(){
			return theString.length;
		}
		public String result(){
			return new String(result);
		}
		public String toString(){
			return new String(theString);
		}
//  )
	// ########## Mutators ########## (
		public MyString left(int size){
			int stringLength = theString.length;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				for(int i=0; i<stringLength; i++){
					result[i] = theString[i];
				}
				for(int j=stringLength; j<size; j++){
					result[j] = ' ';
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int i=0; i<size; i++){
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString right(int size){
			int stringLength = theString.length;
			int emptySpace = size - stringLength;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				for(int j=0; j<emptySpace; j++){
					result[j] = ' ';
				}
				int count = 0;
				for(int i=emptySpace; i<size; i++){
					result[i] = theString[count];
					count++;
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<size; j++){
					result[j] = theString[j];
				}
			}
			return new MyString(result);
		}
		public MyString mid(int size){
			int stringLength = theString.length;
			int emptySpace = size - stringLength;
			int leftPadding=0, rightPadding=0;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				if ((emptySpace % 2) == 0){ // If the empty space size is even...
					leftPadding = (emptySpace / 2); // The left padding is equal to the right padding.
					rightPadding = leftPadding;
				}
				if ((emptySpace % 2) == 1){ // If the empty space size is odd...
					rightPadding = (emptySpace / 2) + 1; // The left padding is smaller than the right by 1 space.
					leftPadding = emptySpace / 2;
				}
				for(int i=0; i<leftPadding; i++){
					result[i] = ' ';
				}
				int count = 0;
				for(int j=leftPadding; j<(leftPadding + stringLength); j++){
					result[j] = theString[count];
					count++;
				}
				for(int k=(leftPadding + stringLength); k<size; k++){
					result[k] = ' ';
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<size; j++){
					result[j] = theString[j];
				}
			}
			return new MyString(result);
		}
		public boolean hasPattern(String s){ // Determines whether or not a search term exists in our string (true or false).
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so break...
			}
			return found;
		}
		public int firstOcurrenceOf(String s, int n){ // Returns the position at which the search term is first encountered. The search begins at the desired starting point n.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int start = n; // The location in in our string where we begin the search.
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=start; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength || start >= stringLength) { // If our search term is bigger than our main string itself, or the starting point is outside the string's boundaries...
					break; // Then obviously our search term will not be found, so prevent the CPU from wasting it's resources on the test or encountering errors...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so exit the loop...
			}
			return foundAt;
		}
		public int firstOcurrenceOf(String s){ // This is simply an overloaded version of the previous method so that if you leave off the "n" parameter, it defaults to 0.
			return firstOcurrenceOf(s, 0);
		}
		public int[] findAll(String s){ // Returns an array containing all the locations in our main string where a search term is found to begin.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			ArrayList<Integer> locations = new ArrayList<Integer>(); // Locations where a match is found...
			int foundAt[]; // The final array to output containing the locations of the ArrayList above...
			int charMatches = 0; // Counter for matching characters...
			
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					found = false; // Set to false for each test unless we find a matching term...
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						locations.add(i); // The match is at position i. Add it to our ArrayList.
					}
				}
			}
			int termCount = locations.size();
			foundAt = new int[termCount];
			for (int h=0; h<termCount; h++) {
				foundAt[h] = locations.get(h);
			}
			return foundAt;
		}
		public MyString uppercase(){ // Turns the string to all uppercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 97 && theString[i] <= 122) {
					result[i] = (char)(theString[i] - ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString lowercase(){ // Turns the string to all lowercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 65 && theString[i] <= 90) {
					result[i] = (char)(theString[i] + ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
			return new MyString(result);
		}
		public MyString invert(){ // Inverts the string so it is spelled backwards.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=stringLength-1; i>=0; i--) {
				result[ (stringLength-1)-i ] = theString[i];
			}
			return new MyString(result);
		}
//  )
}

Revision: 33786
at October 13, 2010 09:28 by trusktr


Initial Code
import java.util.ArrayList;

public class MyString{
	
	// ########## Data Objects ########## (
		private char[] theString, result;
//  )
	// ########## Constructors ########## (
		public MyString(){ // default
			theString = new char[] {'x'};
			result = new char[] {'x'};
		}
		public MyString(String s){ // parameterized
			theString = s.toCharArray();
			result = new char[] {'x'};
		}
		public MyString(char[] s){ // parameterized
			int length = s.length;
			theString = new char[length];
			for(int i=0; i<length; i++){
				theString[i] = s[i];
			}
			result = new char[] {'x'};
		}
		public MyString(MyString s){ // copy
			theString = s.contents();
			result = new char[] {'x'};
		}
//	)
	// ########## Accessors ########## (
		private char[] contents(){
			char[] s = theString;
			return s;
		}
		public String myString(){
			String output = new String(theString);
			return output;
		}
		public String result(){
			String output = new String(result);
			return output;
		}
		public String toString(){
			String output = new String(result);
			return output;
		}
//  )
	// ########## Mutators ########## (
		public void left(int size){
			int stringLength = theString.length;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				for(int i=0; i<stringLength; i++){
					result[i] = theString[i];
				}
				for(int j=stringLength; j<size; j++){
					result[j] = ' ';
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int i=0; i<size; i++){
					result[i] = theString[i];
				}
			}
		}
		public void right(int size){
			int stringLength = theString.length;
			int emptySpace = size - stringLength;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				for(int j=0; j<emptySpace; j++){
					result[j] = ' ';
				}
				int count = 0;
				for(int i=emptySpace; i<size; i++){
					result[i] = theString[count];
					count++;
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<size; j++){
					result[j] = theString[j];
				}
			}
		}
		public void mid(int size){
			int stringLength = theString.length;
			int emptySpace = size - stringLength;
			int leftPadding=0, rightPadding=0;
			result = new char[size];
			if(size > stringLength){ 				// If the space is bigger than the string...
				if ((emptySpace % 2) == 0){ // If the empty space size is even...
					leftPadding = (emptySpace / 2); // The left padding is equal to the right padding.
					rightPadding = leftPadding;
				}
				if ((emptySpace % 2) == 1){ // If the empty space size is odd...
					rightPadding = (emptySpace / 2) + 1; // The left padding is smaller than the right by 1 space.
					leftPadding = emptySpace / 2;
				}
				for(int i=0; i<leftPadding; i++){
					result[i] = ' ';
				}
				int count = 0;
				for(int j=leftPadding; j<(leftPadding + stringLength); j++){
					result[j] = theString[count];
					count++;
				}
				for(int k=(leftPadding + stringLength); k<size; k++){
					result[k] = ' ';
				}
			}
			if(size <= stringLength){ 				// If the space is the same size as the string or smaller...
				for(int j=0; j<size; j++){
					result[j] = theString[j];
				}
			}
		}
		public boolean hasPattern(String s){ // Determines whether or not a search term exists in our string (true or false).
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so break...
			}
			return found;
		}
		public int firstOcurrenceOf(String s, int n){ // Returns the position at which the search term is first encountered. The search begins at the desired starting point n.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			int start = n; // The location in in our string where we begin the search.
			int foundAt = 0; // Location where a match is found...
			int charMatches = 0; // Counter for matching characters...
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=start; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength || start >= stringLength) { // If our search term is bigger than our main string itself, or the starting point is outside the string's boundaries...
					break; // Then obviously our search term will not be found, so prevent the CPU from wasting it's resources on the test or encountering errors...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						foundAt = i; // The match is at position i!
					}
				}
				if(found){break;} // We just want to find the first ocurrence for now, so exit the loop...
			}
			return foundAt;
		}
		public int firstOcurrenceOf(String s){ // This is simply an overloaded version of the previous method so that if you leave off the "n" parameter, it defaults to 0.
			return firstOcurrenceOf(s, 0);
		}
		public int[] findAll(String s){ // Returns an array containing all the locations in our main string where a search term is found to begin.
			char[] searchString = s.toCharArray();
			int searchLength = searchString.length;
			int stringLength = theString.length;
			ArrayList<Integer> locations = new ArrayList<Integer>(); // Locations where a match is found...
			int foundAt[]; // The final array to output containing the locations of the ArrayList above...
			int charMatches = 0; // Counter for matching characters...
			
			boolean found = false; // To stop the test when a match is found...
			
			for(int i=0; i<stringLength; i++){ // For each letter in our string...
				if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
					break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
				}
				if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
					int k=i; // To count starting at our current position without messing up i.
					charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
					for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
						if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
							break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
						}
						if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
							charMatches++; // Then count a character match.
						}
						k++; // Move to the next letter in our base string...
					}
					found = false; // Set to false for each test unless we find a matching term...
					if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
						found = true; // Then we've found a matching sequence of characters!
						locations.add(i); // The match is at position i. Add it to our ArrayList.
					}
				}
			}
			int termCount = locations.size();
			foundAt = new int[termCount];
			for (int h=0; h<termCount; h++) {
				foundAt[h] = locations.get(h);
			}
			return foundAt;
		}
		public void uppercase(){ // Turns the string to all uppercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 97 && theString[i] <= 122) {
					result[i] = (char)(theString[i] - ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
		}
		public void lowercase(){ // Turns the string to all lowercase.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=0; i<stringLength; i++) {
				if (theString[i] >= 65 && theString[i] <= 90) {
					result[i] = (char)(theString[i] + ('a' - 'A'));
				}
				else {
					result[i] = theString[i];
				}
			}
		}
		public void invert(){ // Inverts the string so it is spelled backwards.
			int stringLength = theString.length;
			result = new char[stringLength];
			for (int i=stringLength-1; i>=0; i--) {
				result[ (stringLength-1)-i ] = theString[i];
			}
		}
//  )
}

Initial URL
atbskate.com/trusktr

Initial Description
Great for formatting.

Initial Title
cisp401 MyString.java

Initial Tags
class

Initial Language
Java