Question: *****I already have this M1 part of the answer, but please help me to modify which part of the code is what I need (string.cpp,

*****I already have this M1 part of the answer, but please help me to modify which part of the code is what I need (string.cpp, string.hpp, Makefile) for M1 requirements.

*****I already have this M1 part of the answer, but please help

string.cpp

#include "string.h"

/* * Default constructor for string * Ex: String str; * Ex: String str(); */ String::String(const int cap) { capacity = cap; s = new char[capacity]; length = 0; s[0] = '\0'; }

/* * String constructor for a single char * Ex: String str = 'a'; * Ex: String str('a'); */ String::String(const char rhs, const int cap) { capacity = cap; s = new char[capacity]; length = 1; s[0] = rhs; s[1] = '\0'; }

/* * String constructor for C-Style strings * Ex: String str = "abc"; * Ex: String str("abc"); */ String::String(const char rhs[], const int cap) { // Calculate the length of the string for (length = 0; rhs[length] != '\0'; ++length) {}

// Resize the capacity as needed if (length > cap - 1) { capacity = length + 1; } else { capacity = cap; }

s = new char[capacity];

// Copy chars to string for (int index = 0; rhs[index] != '\0'; ++index) { s[index] = rhs[index]; }

s[length] = '\0'; }

/* * Copy constructor for string * Ex: String str = strcopy; */ String::String(const String& rhs, const int cap) { // Check if capacity is shorter than length if (cap

s = new char[capacity];

// Copy chars to string for (length = 0; rhs.s[length] != '\0'; ++length) { s[length] = rhs.s[length]; }

s[length] = '\0'; }

/* * Destructor for String */ String::~String() { length = 0; delete [] s; }

/* * Assignment operator for string * Ex: String str = rhs_str; */ String& String::operator = (String rhs) { swap(rhs); return *this; }

/* * Compares two strings together * Ex: str1 == str2; */ bool String::operator == (const String& rhs) const { if (length != rhs.length) { return false; }

for (int index = 0; index

return true; }

/* * Compares to see if two strings are less than each other * Ex: str1

for (; s[index] != '\0' && rhs.s[index] != '\0'; ++index) { if (s[index] > rhs.s[index]) { return false; }

if (s[index]

if (lessThan && (s[index] == '\0') && (rhs.s[index] == '\0')) { return true; }

if (lessThan && (rhs.s[index] == '\0')) { return true; }

if (!lessThan && (s[index] == '\0') && (rhs.s[index] == '\0')) { return false; }

if (s[index] == '\0') { return true; }

return false; }

/* * Adds two strings together * Ex: String result = left + right; */ String String::operator + (const String& rhs) const { String result(*this, length + rhs.length + 1); result.length = length + rhs.length;

for (int rhsindex = 0, index = length; rhsindex

result.s[result.length] = '\0';

return result; }

/* * Subtracts a specified number of chars from the end of a string * Ex: str = str1 - 5; * Ex: str -= 5; */ String String::operator - (const int x) const { if (x == 0) { return *this; }

String result;

if (x

return result; }

/* * Subtracts all instances of a char from a string * Ex: str - 'a'; * Ex: str -= 'a'; */ String String::operator - (const char ch) const { if (findchar(ch) == -1) { return *this; }

String result;

for (int index = 0; index

return result; }

/* * Repeats a string a specified number of times with the multiplication operator * Ex: str = str1 * 5; */ String String::operator * (const int x) const { String result(length * x + 1);

for (int index = 0; index

return result; }

/* * Cuts a specified number of chars from the front of a string * Ex: str = str1 / 3; */ String String::operator / (const int x) const { String result(capacity - x);

for (int index = x; index

return result; }

/* * Returns the character from a specified index. Returns null if out of bounds. * Ex: char c = str[1]; */ char String::operator [] (const int index) const { try { return s[index]; } catch (const std::out_of_range& oor) { std::cerr

char& String::operator [] (const int index) { try { return s[index]; } catch (const std::out_of_range& oor) { std::cerr

/* * Inputs string with the >> operator * Ex: std::cin >> str; */ std::istream& operator >> (std::istream& in, String& rhs) { char temp;

while (in) { in.get(temp);

if (!in.eof()) { rhs += temp; } }

return in; }

/* * Outputs string with

return out; }

/* * Finds the first occurance of a char in a string with zero offset * Ex: str.findchar('c'); * Ex: str.findchar('c', 10); */ int String::findchar(const char find, const int offset, const bool polarity) const { for (int index = offset; index

return -1; }

/* * Finds how many times a string occurs in another string * Ex: str.findstr(find); */ int String::findstr(const String& find) const { if (find == *this) { return 1; } else if (length

int result = 0;

for (int index = findchar(find.s[0]); index

return result; }

/* * Justifies a string to a specified width * Ex: str.justify(50); */ String String::justify(const int width) const { String result = *this; int index = 0;

while (result.length

return result; }

/* * Finds the next blank space in a string * Ex: str.nextBlank(); * Ex: str.nextBlank(3); */ int String::nextBlank(const int start) const { if (findchar(' ', 0) == -1) { return -1; }

for (int index = start; true; ++index) { if (index >= length) { index = 0; }

if (s[index] == ' ') { return index; } }

return -1; }

int String::nextNonBlank(const int start) const { if (findchar(' ', 0, false) == -1) { return -1; }

for (int index = start; true; ++index) { if (index >= length){ index = 0; }

if (s[index] != ' ') { return index; } }

return -1; }

/* * Reallocates string's capacity to a specified value * Ex: str.reallocate(40); */ void String::reallocate(const int cap) { String temp(*this, cap); swap(temp); }

/* * Replaces a char in a string with another char * Ex: str.replaceChar(' ', ' '); */ String String::replaceChar(const char find, const char replace) const { String result(*this, length + 1);

for (int index = 0; index

return result; }

String String::replaceChar(const char find, const String& replace) const { String result;

for (int index = 0; index

return result; }

/* * Reverses the contents of a string * Ex: str.reverse(); */ String String::reverse() const { String result(length + 1);

for (int index = length - 1; index >= 0; --index) { result += s[index]; }

return result; }

/* * Splits a string on a specified char. Defaults to every char * Ex: str.split(); * Ex: str.split(' '); * Ex: str.split('a'); */ std::vector String::split(const char ch) const { std::vector result;

if (ch == '\0') { for (int index = 0; index

return result; }

String temp;

for (int index = 0; index

return (result.push_back(temp), result); }

/* * Swaps two strings * Ex: str1.swap(str2); */ void String::swap(String& str) { char *temp = s; s = str.s; str.s = temp;

int temp_cap = capacity; capacity = str.capacity; str.capacity = temp_cap;

int temp_length = length; length = str.length; str.length = temp_length; }

/* * Extracts the selected parts of the string based off of an index and length * Ex: str.substr(1); * Ex: str.substr(1, 5); */ String String::substr(int left, int right) const { String result;

if (left

if (right == 0 || (left + right) > length) { right = length; } else { right += left; }

for (; left

return result; }

/* * Zips two string together like a zipper * Ex: str.zip(str2); */ String String::zip(const String& rhs) const { String result(length + rhs.length + 1);

for (int index = 0; index

if (index

return result; }

string.hpp

#ifndef ELIGUNDRY_STRING_H_ #define ELIGUNDRY_STRING_H_

#include #include #include #include

#define DEFAULT_STRING_CAPACITY 128

class String { public: /* * Default constructor for string with optional capacity * Ex: String str; * Ex: String str(); * Ex: String str(28); */ String(int cap = DEFAULT_STRING_CAPACITY);

/* * String constructor for a single char * Ex: String str = 'a'; * Ex: String str('a'); * Ex: String str('a', 28); */ String(const char, int cap = DEFAULT_STRING_CAPACITY);

/* * String constructor for C-Style strings * Ex: String str = "abc"; * Ex: String str("abc"); * Ex: String str("abc", 100); */ String(const char[], int cap = DEFAULT_STRING_CAPACITY);

/* * Copy constructor for string * Ex: String str = strcopy; * Ex: String str(strcopy, 50); */ String(const String&, int cap = DEFAULT_STRING_CAPACITY);

/* * Destructor for String */ ~String();

/* * Copy operator for string * Ex: String str = rhs_str; */ String& operator = (String);

/* * Compares two strings together * Ex: str1 == str2; * Ex: str1 != str2; */ bool operator == (const String&) const; bool operator != (const String& rhs) const { return !(*this == rhs); };

/* * Compares a string to a character array * Ex: str == 'abc'; * Ex: str != 'abc'; */ bool operator == (const char rhs[]) const { return *this == String(rhs); }; bool operator != (const char rhs[]) const { return !(*this == String(rhs)); }; bool operator == (const char rhs) const { return *this == String(rhs); }; bool operator != (const char rhs) const { return !(*this == String(rhs)); };

/* * Compares to see if two strings are greater or less than * Ex: str1 str2; * Ex: str1 >= str2; * Ex: str1 (const String& rhs) const { return rhs = (const String& rhs) const { return (rhs

/* * Adds two strings together * Ex: String result = left + right; * Ex: String str1 += str2; * Ex: String result = left + "!!"; * Ex: String result = left + '!'; */ String operator + (const String&) const; String operator + (const char rhs[]) const { return *this + String(rhs); }; String operator + (char rhs) const { return *this + String(rhs); }; String& operator += (const String& rhs) { return *this = *this + rhs; }; String& operator += (const char rhs[]) { return *this = *this + String(rhs); }; String& operator += (char rhs) { return *this = *this + String(rhs); };

/* * Friend functions to add a char or char[] in front of a string * Ex: str = 't' + rhs; * Ex: str = "the " + rhs; */ friend String operator + (const char lhs, const String& rhs) { return String(lhs) + rhs; }; friend String operator + (const char lhs[], const String& rhs) { return String(lhs) + rhs; };

/* * Subtracts a specified number of chars from the end of a string * Ex: str = str1 - 5; * Ex: str -= 5; */ String operator - (const int) const; String& operator -= (const int x) { return *this = *this - x; };

/* * Subtracts all instances of a char from a string * Ex: str - 'a'; */ String operator - (const char) const; String& operator -= (const char ch) { return *this = *this - ch; };

/* * Repeats a string a specified number of times with the multiplication operator * Ex: str = str1 * 5; * Ex: str *= 5; */ String operator * (const int) const; String& operator *= (const int x) { return *this = *this * x; };

/* * Cuts a specified number of chars from the front of a string * Ex: str = str1 / 3; * Ex: str /= 3; */ String operator / (const int) const; String& operator /= (const int x) { return *this = *this / x; };

/* * Returns the character from a specified index. Returns null if out of * bounds. * Ex: char c = str[1]; */ char operator [] (const int) const; char& operator [] (const int);

/* * Inputs string with the >> operator * Ex: std::cin >> str; */ friend std::istream& operator >> (std::istream&, String&);

/* * Outputs string with

/* * Finds the first occurance of a char in a string with zero offset * Ex: str.findchar('c'); * Ex: str.findchar('c', 10); */ int findchar(const char, int offset = 0, bool polarity = true) const;

/* * Finds how many times a string occurs in another string * Ex: str.findstr(find); */ int findstr(const String&) const;

/* * Returns the length of the string * Ex: int x = str.getLength(); */ int getLength() const { return length; };

/* * Returns the memory capacity of the string * Ex: int x = str.getCapacity(); */ int getCapacity() const { return capacity; };

/* * Checks if null character is at end of string * Ex: str.isClosed() == true; */ bool isClosed() const { return s[length] == '\0'; };

/* * Justifies a string to a specified width * Ex: str.justify(50); */ String justify(const int) const;

/* * Finds the next blank space in a string * Ex: str.nextBlank(); * Ex: str.nextBlank(3); */ int nextBlank(int start = 0) const;

/* * Finds the next non-blank char in a string * Ex: str.nextNonBlank(); * Ex: str.nextNonBlank(3); */ int nextNonBlank(int start = 0) const;

/* * Reallocates string's capacity to a specified value * Ex: str.reallocate(40); */ void reallocate(const int);

/* * Replaces a char in a string with another char or string * Ex: str.replaceChar(' ', ' '); */ String replaceChar(const char, const char) const; String replaceChar(const char, const String&) const;

/* * Reverses the contents of a string * Ex: str.reverse(); */ String reverse() const;

/* * Splits a string on a specified char. Defaults to every char * Ex: str.split(); * Ex: str.split(' '); * Ex: str.split('a'); */ std::vector split(char ch = '\0') const;

/* * Strips NL chars from a string * Ex: str.strip_nl(); */ String strip_nl() const { return *this - ' '; };

/* * Swaps two strings * Ex: str1.swap(str2); */ void swap(String&);

/* * Extracts the selected parts of the string based off of 1 or 2 indexes * Ex: str.substr(1); * Ex: str.substr(1, 5); */ String substr(int, int right = 0) const;

/* * Zips two string together like a zipper * Ex: str.zip(str2); */ String zip(const String&) const;

private: char *s; int length; int capacity;

};

#endif

makefile

############################################################### # Justify & String # # CS 23001 Kent State University # Make file for Project 2 # # This makefile will work for all parts of the project. # However, you will need to modify the "tests" rules as # you add new test cases. # 1) add test case to "Variables" section # 2) add test to "Run all tests" section # 3) add test to "clean" #

############################################################### # Variables CPP = g++ OPTIONS = -g -Wall -W -Wunused -Wuninitialized -Wshadow VALGRIND = valgrind -q

CONSTRUCTORS = test_default_ctor.out test_cstring_ctor.out test_copy_constructor.out OPERATORS = test_relational_operators.out test_add.out SEARCH = test_findstring.out test_findchar.out test_substr.out test_strip_nl.out IO = test_output.out test_input.out DYNAMIC = test_swap.out test_force_overflow.out test_reallocate.out FUN = test_subtract.out test_division.out test_multiply.out test_zip.out test_reverse.out test_split.out JUST = test_next_blank.out test_replace_char.out test_justify.out ############################################################### # The first rule is run if only make is typed msg: @echo 'Targets are:' @echo ' string' @echo ' tests' @echo ' valgrind' @echo ' justify' @echo ' clean'

############################################################### # Run all tests

string: $(CONSTRUCTORS) $(OPERATORS) $(SEARCH) $(IO) $(DYNAMIC) $(FUN) $(JUST) ./test_default_ctor.out ./test_cstring_ctor.out ./test_relational_operators.out ./test_add.out ./test_subtract.out ./test_multiply.out ./test_division.out ./test_findchar.out ./test_findstring.out ./test_substr.out ./test_output.out ./test_input.out ./test_swap.out ./test_reallocate.out ./test_force_overflow.out ./test_zip.out ./test_reverse.out ./test_next_blank.out ./test_replace_char.out ./test_split.out ./test_justify.out make clean

tests: make string

valgrind: $(CONSTRUCTORS) $(OPERATORS) $(SEARCH) $(IO) $(DYNAMIC) $(FUN) $(JUST) $(VALGRIND) ./test_default_ctor.out $(VALGRIND) ./test_cstring_ctor.out $(VALGRIND) ./test_relational_operators.out $(VALGRIND) ./test_add.out $(VALGRIND) ./test_subtract.out $(VALGRIND) ./test_multiply.out $(VALGRIND) ./test_division.out $(VALGRIND) ./test_findchar.out $(VALGRIND) ./test_findstring.out $(VALGRIND) ./test_substr.out $(VALGRIND) ./test_output.out $(VALGRIND) ./test_input.out $(VALGRIND) ./test_swap.out $(VALGRIND) ./test_reallocate.out $(VALGRIND) ./test_force_overflow.out $(VALGRIND) ./test_zip.out $(VALGRIND) ./test_reverse.out $(VALGRIND) ./test_next_blank.out $(VALGRIND) ./test_replace_char.out $(VALGRIND) ./test_split.out $(VALGRIND) ./test_justify.out make clean

############################################################### # Compile string # string.o: string.h string.cpp $(CPP) -c $(OPTIONS) string.cpp

############################################################### # Compile Justify # justify: make string.o make justify.o make compile_justify ./justify.out 10 50 data2-1.txt output.txt make clean

compile_justify: string.h string.cpp justify.cpp $(CPP) $(OPTIONS) -o justify.out justify.o string.o

justify.o: justify.cpp string.h $(CPP) -c $(OPTIONS) justify.cpp

############################################################### # Compile all test programs # test_%.out: string.o test_%.o $(CPP) string.o test_$*.o -o test_$*.out

test_%.o: string.h test_%.cpp $(CPP) $(OPTIONS) -c test_$*.cpp

############################################################### # clean # Removes all .o files and all executables # # You will need to add your other test cases executables here. # For example: test_plus clean: rm -f *.o *.out

Objectives: -Use a well developed ADT class which utilizes operator overloading -Use dynamically allocated arrays Problem: The Apache web server produces information detailing web page accesses, this information is stored in a log file in the Apache Common log file format. The assignment is to develop a program to process this log file and keep track of the different types of log entries Requirements * You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified * You must use your ADT string for the later parts of the assignment. *using namespace std: is stricly forbiden. As are any global using statements . Name the folder for this project: string (please use all lower case letters). . Milestone 1 (25pts) o Implementation " Create an ADT string using the class construct. It will be a NULL (zero) terminating charater array Note: C++ has a standard type called string so you should not use this name. Use string instead Please name all your files using only lower case letters Use the provided specification (svn/shared/project2/string-mile1.hpp) for naming your class and methods You should rename this to string.hpp. A test suite will be provided in Part 2 that uses this interface to test your string class. You should use a fixed sized array of char for storage with a max capacity based on a constant const int STRING-SIZE = 256; This array will store the characters along with the NULL (0) terminator. - Implement the constructor functions (i.e., for char] and char) - Overload and+as concatenation (make sure they works for all variations string+ string, string +charl, char[l + string, etc). Overload all the relational operators (=., etc.). Implement the methods operator [] (int) -both accessor and modifier versions " length ) returns number of characters in string - capacity) returns the max number of characters that can be stored in the string substr(int start, int end) returns the sub string from start to end position (inclusive) findch (int pos, char ch returns location of ch at or after pos " findsstr(int pos, cosnt string& str) - returns the location of str at or after pos Overload both I/O operators-Input should read in one word at a time. The input operator for char[] works that way and can be used o Testing Develop a set of test cases, using asserts, for each of the operators and methods of the string class Write test cases first. Testing must be thorough. You will be relying on the string to be correct. The command make tests will build and run the unit tests After each function passes a test, commit your work to svn with a message such as "Constructor tests passed" . Your string class will be tested on a set of cases developed by the instructors. You will be graded on how well you pass these tests. These tests cover the constructors, comparison, operator[l. substr, findch, and the concatenation operator. Objectives: -Use a well developed ADT class which utilizes operator overloading -Use dynamically allocated arrays Problem: The Apache web server produces information detailing web page accesses, this information is stored in a log file in the Apache Common log file format. The assignment is to develop a program to process this log file and keep track of the different types of log entries Requirements * You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified * You must use your ADT string for the later parts of the assignment. *using namespace std: is stricly forbiden. As are any global using statements . Name the folder for this project: string (please use all lower case letters). . Milestone 1 (25pts) o Implementation " Create an ADT string using the class construct. It will be a NULL (zero) terminating charater array Note: C++ has a standard type called string so you should not use this name. Use string instead Please name all your files using only lower case letters Use the provided specification (svn/shared/project2/string-mile1.hpp) for naming your class and methods You should rename this to string.hpp. A test suite will be provided in Part 2 that uses this interface to test your string class. You should use a fixed sized array of char for storage with a max capacity based on a constant const int STRING-SIZE = 256; This array will store the characters along with the NULL (0) terminator. - Implement the constructor functions (i.e., for char] and char) - Overload and+as concatenation (make sure they works for all variations string+ string, string +charl, char[l + string, etc). Overload all the relational operators (=., etc.). Implement the methods operator [] (int) -both accessor and modifier versions " length ) returns number of characters in string - capacity) returns the max number of characters that can be stored in the string substr(int start, int end) returns the sub string from start to end position (inclusive) findch (int pos, char ch returns location of ch at or after pos " findsstr(int pos, cosnt string& str) - returns the location of str at or after pos Overload both I/O operators-Input should read in one word at a time. The input operator for char[] works that way and can be used o Testing Develop a set of test cases, using asserts, for each of the operators and methods of the string class Write test cases first. Testing must be thorough. You will be relying on the string to be correct. The command make tests will build and run the unit tests After each function passes a test, commit your work to svn with a message such as "Constructor tests passed" . Your string class will be tested on a set of cases developed by the instructors. You will be graded on how well you pass these tests. These tests cover the constructors, comparison, operator[l. substr, findch, and the concatenation operator

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!