rendered paste body/* * stringPedro.cpp * * Author: Pedro de la Cruz * University of Houston * Data Structures, Fall 2009 * */#include "stringPedro.h"/* Constructors */StringPedro::StringPedro() { data = ""; }StringPedro::StringPedro(const char* string) { data = string; StringPedro::size(); }/* Destructor */StringPedro::~StringPedro() { }/* Public Methods */int StringPedro::size() { for(stringSize = 0; data[stringSize]; stringSize++) { } return stringSize; }void StringPedro::output() { cout << data << endl; }/* * Make the string into decimal notation * and places each decimal into a * "slot" in a type int array * */int * StringPedro::decimal() { int * myArray = (int*) malloc(stringSize*sizeof(int)); for(int i = 0; i < stringSize; i++) { if((data[i]%2) == 0) { myArray[i] = (data[i]/2)*2; } else { myArray[i] = (data[i]/2)*2 + 1; } } return myArray;}/* * Returns 1 if the string are the same * otherwise returns 0 * */int StringPedro::compare(StringPedro str1, StringPedro str2) { int * str1_decimal = str1.decimal(); int * str2_decimal = str2.decimal(); int str1_Size = str1.size(); int str2_Size = str2.size(); /* * First check to see if * the two strings are the * same size */ if(str1_Size == str2_Size) { for(int j = 0; j < str1_Size; j++) { if(str1_decimal[j] != str2_decimal[j]) { return 0; } } return 1; } else { return 0; } }void StringPedro::findAll(StringPedro string, StringPedro searchWord ,int start, int * matches, int * partial) { *matches = 0; *partial = 0; int * string_decimal = string.decimal(); int * searchWord_decimal = searchWord.decimal(); int j = start; /* Search loop */ while(j < string.size()) { /* * First letter is found * continue to check it out * otherwise j++ */ if(string_decimal[j] == searchWord_decimal[0]) { int i=1; while(i < searchWord.size()) { if(string_decimal[j+i] == searchWord_decimal[i]) i++; else break; } if(i == searchWord.size()) { *matches = *matches + 1; j = j + i - 1; } /* Partial found update variable */ *partial = *partial + 1; } j++; }}void StringPedro::findAll(StringPedro string, StringPedro searchWord , int * matches) { *matches = 0; int * string_decimal = string.decimal(); int * searchWord_decimal = searchWord.decimal(); int j = 0; /* Search loop */ while(j < string.size()) { /* * First letter is found * continue to check it out * otherwise j++ */ if(string_decimal[j] == searchWord_decimal[0]) { int i=1; while(i < searchWord.size()) { if(string_decimal[j+i] == searchWord_decimal[i]) i++; else break; } if(i == searchWord.size()) { *matches = *matches + 1; j = j + i - 1 ; } } j++; }}/* Find the first occurance * of the word 'string' */void StringPedro::find(StringPedro string, StringPedro searchWord ,int * position) { if(*position < 0) { cout << "Can not continue. Find start position is negative" << endl; exit(0); } else if(*position > string.size()) { cout << "Can not continue. Find start position is above the search string amount" << endl; exit(0); } else if(*position == string.size()) { cout << "Can not continue. Find start position is equal the search string amount" << endl; exit(0); } int * string_decimal = string.decimal(); int * searchWord_decimal = searchWord.decimal(); int j = *position; /* Search loop */ while(j < string.size()) { /* * First letter is found * continue to check it out * otherwise j++ */ if(string_decimal[j] == searchWord_decimal[0]) { int i=1; while(i < searchWord.size()) { if(string_decimal[j+i] == searchWord_decimal[i]) i++; else break; } if(i == searchWord.size()) { *position = j; break; } } j++; }}StringPedro StringPedro::replace(StringPedro str1, StringPedro str2, StringPedro str3) { StringPedro str4; int matches; int i = 0; int n = 0; StringPedro::findAll(str1, str2, &matches); /* * The formula for the size * of the new array */ int arraySize = str1.size() + matches*(str3.size() - str2.size()); char* string4; if(arraySize > 0) { string4 = (char*) malloc(arraySize*sizeof(char)); } else { cout << "Replace array size is negative. Can not continue" << endl; exit(0); } StringPedro::find(str1, str2, &n); while(i < arraySize) { if(i == n) { str4 = str4 + str3; i = i + str2.size(); n = n + str2.size()-1; StringPedro::find(str1, str2, &n); if(n+i>arraySize) break; } else { const char character = str1.data[i]; StringPedro tmp(&character); str4 = str4 + tmp; i++; } } return str4;}void StringPedro::set(const char* string) { data = NULL; data = string; StringPedro::size();}void StringPedro::setLetter(const char value) { data = &value;}StringPedro StringPedro::output(int start, int end) { int i; StringPedro result; for(i = start; i <= end; i++) { result = result + data[i]; } return result;}int StringPedro::integer() { int result; result = atoi(data); return result;}/* * Idea is * str1.data + str2.data = str3.data * */StringPedro operator +(StringPedro& str1, StringPedro& str2) { char* string3 = (char*) malloc((str1.size() + str2.size())*sizeof(char)); int j = 0; /* * Loops through each letter of each word */ for(int i = 0; i < str1.size() + str2.size() ; i++) { if(i < str1.size()) string3[i] = str1.data[j]; else string3[i] = str2.data[j]; if(i == str1.size() - 1 ) { j = 0; } else { j++; } } StringPedro str3(string3); return str3;}StringPedro operator +(StringPedro& str1, const char letter) { char* string3 = (char*) malloc((str1.size() + sizeof(char))); int j = 0; /* * Loops through each letter of each word */ for(int i = 0; i < str1.size() + 1 ; i++) { if(i < str1.size()) string3[i] = str1.data[j]; else string3[i] = letter; if(i == str1.size() - 1 ) { j = 0; } else { j++; } } StringPedro str3(string3); return str3;}