Lenguage C++ Task Create a class called MyString -- this will be a string class,
ID: 3911365 • Letter: L
Question
Lenguage
C++
Task
Create a class called MyString -- this will be a string class, which allows creation of string objects that have flexible sizes, intuitive operator syntax (through operator overloads), and other useful features. Your class will need to maintain the string internally as an array of characters, and since string sizes are not fixed, dynamic allocation techniques will need to be used in the class. Your class should maintain any string ojbect in a valid state at all times, and it should not allow any memory leaks.
The class should be written in the files mystring.h and mystring.cpp. I have provided a starter version of the file mystring.h, which already has many of the required features (i.e. the interface for users) declared. Do not change the prototypes of the already-declared functions.
Important Note: Since the intention of this assignment is for you to write a string class, you may NOT use the <string> class library in the creation of this assignment! Use of the standard C++ <string> library will result in an automatic 0 grade. The point here is to learn how to go about building such a library yourself.
Details and Requirements
Data
Your class must allow for storage of a flexibly-sized string of characters. Make sure to declare any appropriate member data variables in the header file. All member data of your class must be private.
Function descriptions
Standard Constructors
The default constructor should set the object to represent an empty string.
MyString(const char* ) -- this is a conversion constructor. A c-string will be passed as a parameter, and this constructor should set up the string object to store that string inside. This will enable type conversions from c-strings to MyString objects.
MyString(int ) -- this is a conversion constructor that should convert an integer value to a string representation. For example, if the value 1234 is passed in, the MyString object should now store the same string data that would be represented by the c-string "1234"
Note that these last two constructors will allow automatic type conversions to take place -- in this case, conversions from int to MyString and from c-style strings to type MyString -- when appropriate. This makes our operator overloads more versatile, as well. For example, the conversion constructor allows the following statements to work (assuming appropriate definitions of the assignment operator and + overloads described later):
Automatics
Since dynamic allocation is necessary, you will need to write appropriate definitions of the special functions (the "automatics"): destructor, copy constructor, assignment operator. The destructor should clean up any dynamic memory when a MyString object is deallocated. The copy constructor and assignment operator should both be defined to make a "deep copy" of the object (copying all dynamic data, in addition to regular member data), using appropriate techniques. Make sure that none of these functions will ever allow memory "leaks" in a program.
I/O functions
operator << -- insertion operator. This should print out the data from the MyString object, using the standard insertion operator syntax. There should be no extra formatting -- only the string stored in the object is printed (no newlines, extra spacing, etc).
operator >> -- extraction operator. This should read a string from an input stream. This operator should ignore any leading white space before the string data, then read until the next white space is encountered. Prior data in the MyString object is discarded. Note: Just like the operator>> for c-strings, this will only read one word at time.
getline function. This should read a string from an input stream. This operator should read everything from the input stream (first parameter) into the MyString object (second parameter) until the specified delimiter character is encountered. Notice that if the function is called with 3 parameters, the third parameter is the delimiter. If the function is called with 2 parameters, the delimiter is the newline ' ' by default. Prior data in the MyString object is discarded.
Comparison operators
Write overloads for all 6 of the comparison operators ( < , > , <= , >= , == , != ). Each of these operations should test two objects of type MyString and return an indication of true or false. You are testing the MyString objects for order and/or equality based on the usual meaning of order and equality for c-strings, which is lexicographic ordering. Remember that this is based on the order of the ascii characters themselves, so it's not exactly the same as pure "alphabetical" ordering.
Examples:
Concatenation operators
operator+ -- this should concatenate the two operands together and return a new MyString as a result.
operator+= -- this should concatenate the second operand onto the first one (i.e. changing the first one)
Examples:
Bracket operators
The bracket operator overloads have these prototypes:
Both of these should return the character at the given index position. Note that the first one returns the character by reference, so it allows the slot to be changed. The second returns by const reference and is a const member function and will run in read-only situations -- calls on const objects. Example calls:
Note that since the parameter in each is an unsigned int, it's not possible to have a negative array index passed in. If the index passed to the function is too big (out of bounds for what's currently stored), then:
The read-only/const version should just return the null character ''
The L-value version should resize the stored string to accomodate the specified index. All new slots between the end of the prior string and the new index location should be set to spaces
Examples:
Standard accessors
getLength should return the length of the stored string (i.e. number of characters). For example, "Hello" has 5 characters
getCString should return the actual stored data as a c-string (i.e. null-terminated char array)
substring functions
There are two versions of substring -- a 1 parameter version and a 2-parameter version. Both should return a MyString object that consists of a portion (or "substring") of the original string. Neither should change the calling object. The first parameter represents the starting index of the substring. In the 2-parameter version, the second parameter gives the length of the substring to return (if the length is too long, default to the rest of the string ). In the 1-parameter version, the substring consists of the characters from the start index to the end of the string.
Examples:
insert() function
This function should change the calling object by inserting the data from the second parameter AT the index given by the first parameter. If the index is out of bounds (longer than the string's length), then just insert at the end. This function should also return the calling object.
Examples:
indexOf function
This function should search through the MyString to find the first occurence of the pattern or substring given in the parameter. The function should return the first index where it was found, or it should return -1 if the pattern is NOT found.
Examples:
General Requirements
As usual, no global variables. If you use any constants in the class, make them static
All member data should be private
Use appropriate good programming practices as denoted on previous assignments
Since the only output involved with your class will be in the << overload, your output must match mine exactly when running test programs.
You may NOT use classes from the STL (Standard Template Library) -- this includes class <string> -- as the whole point of this assignment is for you to learn how to manage dynamic memory issues inside of a class yourself, not rely on STL classes or the string class to do it for you.
You may use standard I/O libraries like iostream and iomanip, as well as the common C libraries cstring and cctype
Extra Credit:
Create an overloaded version of the - operator to "subtract" two MyString objects. The meaning of - is that it should return a MyString object that is the result of taking the first string and removing all instances of the second string from it.
Examples:
Hints and Tips:
Converting between ints and characters:
Be aware that 1 and '1' are not the same thing. '1' stores the ascii value of the character representing 1, which happens to be 49. The easiest way to convert between single digit integers and the character code representation is to add or subtract the ascii value of '0'.
Example:
Input:For the >> operator overload as well as the getline function, there are some issues to be careful about. You will not be able to just use the normal version of >> (or getline) for c-strings, because it attempts to read consecutive characters until white space or a delimiter is encountered. The problem here is that we will be entering an unknown number of characters, so you won't be able to allocate ALL of the space in advance. These operations may need to dynamically resize the array as you read characters in.
Sample main program
It will be up to you to write 1 or more test programs to test out the features of your class.
#include iostream» using namespace std; class Mystring friend ostream& operator (istream& , Mystring&); friend istrean& getline (istream& , MyString& , char delim = . "); friend Mystring operator+ (const Mystring&, const MyString&); friend bool operatork (const MyString&, const HyString&); friend bool operator (const Mystring&, const Mystring&); friend bool operator (const MyString&, const HyString&); friend bool operator (const MyString&, const HyString&); friend bool operator(const MyString&, const HyString&); friend bool operator !=(const MyString& , const MyString& ); public: Mystring) Mystring(const char* ); Mystring int); MyString); Mystring (const MyString&j; Mystring&operator-;(const MyString&); // assignment operator // empty string /I conversion from c-string conversion from int /I destructor /I copy constructor Mystring&operator;+ const Mystring& ); // concatenation/assignment // bracket operators to access char positions char& operatorl (unsigned int index); const char& operator[] (unsigned int index) const; // insert s into the string at position "index" Mystring& insert(unsigned int index, const MyString& s); // find index of the first occurrence of s inside the string // return the index, or -1 if not found int indexof (const MyString& s) const; int getLength const; const char getcstring) const; // return string length // return c-string equiv Mystring substring (unsigned int, unsigned int const; Mystring substring(unsigned int) const; private: / declare your member data hereExplanation / Answer
//------------------------------------------------mystring.h-------------------------------------------------
#include <iostream>
using namespace std;
class MyString
{
friend ostream& operator<< (ostream& , const MyString& ); // MyString Object ostream, output format is c-string
friend istream& operator>> (istream& , MyString& ); // MyString Object istream, input will be considered as c-string
friend istream& getline (istream& , MyString& , char delim = ' '); // MyString istream getline function
friend MyString operator+ (const MyString& , const MyString& ); // operator overload, string plus operation
friend MyString operator- (const MyString& , const MyString& ); // operator overload, string minus operation
friend bool operator< (const MyString& , const MyString& ); // string logic operation <
friend bool operator> (const MyString& , const MyString& ); // string logic operation >
friend bool operator<=(const MyString& , const MyString& ); // string logic operation <=
friend bool operator>=(const MyString& , const MyString& ); // string logic operation >=
friend bool operator==(const MyString& , const MyString& ); // string logic operation ==
friend bool operator!=(const MyString& , const MyString& ); // string logic operation !=
public:
MyString(); // empty string
MyString(const char* ); // conversion from c-string
MyString(int ); // conversion from int
~MyString(); // destructor
MyString(const MyString& ); // copy constructor
MyString& operator=(const MyString& ); // assignment operator
MyString& operator+=(const MyString& ); // concatenation/assignment
// bracket operators to access char positions
char& operator[] (unsigned int index);
const char& operator[] (unsigned int index) const;
// insert s into the string at position "index"
MyString& insert(unsigned int index, const MyString& s);
// find index of the first occurrence of s inside the string, return the index, or -1 if not found
int indexOf(const MyString& s) const;
int getLength() const; // return string length
const char* getCString() const; // return c-string equivalent
// return the sub-string of calling object at refered index
MyString substring(unsigned int , unsigned int ) const;
MyString substring(unsigned int ) const;
private:
char* stringentry; // c-string pointer of object
int stringlength; // stringlength, including the '' character
};
//------------------------------------------------mystring.h-------------------------------------------------
#include <iostream>
using namespace std;
#include "mystring.h"
int main()
{
MyString s1;
MyString s2("Hello, World");
MyString s3 = "Welcome to Florida, have a nice day";
MyString s4 = 12345;
MyString ss = "This is Xiangzhen Sun, and I like being successful!!!";
MyString sx = "is";
MyString sy = "e";
MyString s6;
MyString s7 = " wlekglskdgsssss wlekegls";
MyString s8 = " wlek";
cout << "s1 = " << s1 << ' ';
cout << "s2 = " << s2 << ' ';
cout << "s3 = " << s3 << ' ';
cout << "s4 = " << s4 << ' ';
cout << ' ';
cout << "Making the calls: "
<< " cin >> s1 "
<< " getline(cin, s2, ',') "
<< " getline(cin, s3) ";
cout << "Enter some sentences: ";
cin >> s1;
cin >> s6;
getline(cin,s2,',');
getline(cin,s3);
getline(cin,s4,':');
cout << " New string values: ";
cout << "s1 = " << s1 << ' ';
cout << "s6 = " << s6 << ' ';
cout << "s2 = " << s2 << ' ';
cout << "s3 = " << s3 << ' ';
cout << "s4 = " << s4 << ' ';
cout << "--------------------------- ";
// ----------------------------------
s1 = "Dog";
s2 = "Food";
MyString result = s1 + s2;
MyString result1 = s1 - s2;
MyString result2 = s2 - s1;
cout << "result = " << result << ' ';
cout << "result1 = " << result1 << ' ';
cout << "result2 = " << result2 << ' ';
cout << "ss - sx = " << ss - sx << ' ';
cout << "ss - sx - sy = " << ss - sx - sy << ' ';
cout << "s7 - s8 = " << s7 - s8 << ' ';
s1 += s2;
cout << "s1 = " << s1 << endl;
MyString result4 = s1 - s2;
cout << "result4 = " << result4 << ' ';
if (s1 == result4) cout << "s1 == result4 is true ";
const MyString s5 = "The concatenation of the catapult is a catamaran";
cout << "s5 = " << s5 << endl;
cout << "s5.indexOf("cat") returns " << s5.indexOf("cat") << ' ';
cout << "s5.indexOf("dog") returns " << s5.indexOf("dog") << ' ';
cout << "s5.getLength() = " << s5.getLength() << ' ';
cout << "s5[4] = " << s5[4] << ' ';
cout << "s5[10] = " << s5[10] << ' ';
cout << "s5[15] = " << s5[15] << ' ';
cout << "s5[52] = ascii " << static_cast<int>(s5[52]) << ' ';
cout << "s5.substring(10,16) = " << s5.substring(10,16) << ' ';
cout << "s5.substring(23) = " << s5.substring(23) << ' ';
cout << "----------------------------- ";
MyString words = "Greetings, Earthling";
cout << "words = " << words << ' ';
cout << "words.getLength() = " << words.getLength() << ' ';
words[0] = 'K';
words[4] = 'p';
words[16] = 'z';
cout << "words = " << words << ' ';
words[25] = 'Q';
cout << "words = " << words << ' ';
words.insert(11, "Insane ");
cout << "words = " << words << ' ';
cout << "words = " << words.getCString() << ' ';
cout << "----------------------------- ";
MyString x = "apple", y = "apply";
cout << "x = " << x << ' ';
cout << "y = " << y << ' ';
if (x < y) cout << "x < y is true ";
if (x > y) cout << "x > y is true ";
if (x <= y) cout << "x <= y is true ";
if (x >= y) cout << "x >= y is true ";
if (x == y) cout << "x == y is true ";
if (x != y) cout << "x != y is true ";
}
/* Description: This is a self-defined string class. The creation of this class is assigned for
making c-string operation much more comfortable. With this MyString class, you can actually
do string comparision, string addition/subtraction, string I/O, string insertion, let alone
copy, assignment, etc. (Extra Bonus part is completely done) */
//------------------------------mystring.cpp------------------------------
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
#include "mystring.h"
using namespace std;
//-----------------------------------------------------------------------------------------------------
// ostream operator overload, outputs c-string
ostream& operator<< (ostream& os, const MyString& s)
{
os << s.stringentry;
return os;
}
//-----------------------------------------------------------------------------------------------------
// istream operator overload, extracts one char one time, ignoring any white space in front of c-string
istream& operator>> (istream& is, MyString& s)
{
int maxsize = 1;
int index = 0;
char* bufferstring = new char[maxsize]; // buffer extends dimension by 1 when it overflows
while(isspace(is.peek())) // ignore white spaces in front
is.ignore();
while((!isspace(is.peek())) && (!is.eof()))
{
char* tempstring = new char[maxsize + 1];
is >> bufferstring[index];
for(int i = 0; i < maxsize; i++)
tempstring[i] = bufferstring[i];
index++;
maxsize++;
delete [] bufferstring;
bufferstring = tempstring;
}
bufferstring[index] = '';
delete [] s.stringentry; // release memory, clean up dynamic allocation
s.stringentry = bufferstring;
return is;
}
//------------------------------------------------------------------------------------------------------
// istream memberfunction getline, extracts a whole c-string until meeting delim, and discards delim
istream& getline (istream& is, MyString& s, char delim)
{
int index = 0;
int buffersize = 10;
char* bufferstring = new char[buffersize]; //after buffer saturates, buffer grows by 1 each time
do
{
if(!is.eof())
{
if(index < buffersize - 1)
{
bufferstring[index] = is.get();
index++;
}
else
{
buffersize++;
char* tempbuffer = new char[buffersize];
for(int i = 0; i < index; i++)
tempbuffer[i] = bufferstring[i];
tempbuffer[index] = is.get();
index++;
delete [] bufferstring;
bufferstring = tempbuffer;
}
}
}while((is.peek() != delim) && (!is.eof())); // loop terminates at delim or end of file
is.ignore(); // discards delim char, and leaves it for latter
buffersize = index + 1;
bufferstring[index] = '';
delete [] s.stringentry; // release memory
s.stringentry = bufferstring;
s.stringlength = buffersize;
return is;
}
//--------------------------------------------------------------------------------------------------------
// operator overload, string addition, result being returned to new object
MyString operator+ (const MyString& s1, const MyString& s2)
{
MyString s = s1;
s += s2;
return s;
}
//---------------------------------------------------------------------------------------------------------
// operator overload, string subtraction, result being return to new object, Bonus Function!
MyString operator- (const MyString& s1, const MyString& s2)
{
MyString s = s1;
int index = s1.indexOf(s2);
if(index != -1)
{
while(index != -1)
// this loop helps searching along c-string until all substring related to the string being
// subtracted are deleted
{
char* tempstring = new char[s.stringlength - s2.stringlength + 1];
for(int i = 0; i < index; i++)
tempstring[i] = s.stringentry[i];
for(int i = index + s2.stringlength - 1; i < s.stringlength - 1; i++)
tempstring[i - s2.stringlength + 1] = s.stringentry[i];
tempstring[s.stringlength - s2.stringlength] = '';
delete [] s.stringentry;
s.stringentry = tempstring;
s.stringlength = (s.stringlength - s2.stringlength + 1);
index = s.indexOf(s2);
}
return s;
}
else
return s1;
}
//-------------------------------------------------------------------------------------------------------
// logic comparision, boolean return type
bool operator< (const MyString& s1, const MyString& s2)
{
int i = 0;
while((s1.stringentry[i] != '') && (s1.stringentry[i] == s2.stringentry[i]))
i++;
if((s1.stringentry[i] - s2.stringentry[i]) < 0)
return true;
else
return false;
}
//--------------------------------------------------------------------------------------------------------
// logic comparision, boolean return type
bool operator> (const MyString& s1, const MyString& s2)
{
if(s1 <= s2)
return false;
else
return true;
}
//--------------------------------------------------------------------------------------------------------
// logic comparision, boolean return type
bool operator<=(const MyString& s1, const MyString& s2)
{
if((s1 < s2) || (s1 == s2))
return true;
else
return false;
}
//--------------------------------------------------------------------------------------------------------
// logic comparision, boolean return type
bool operator>=(const MyString& s1, const MyString& s2)
{
if(s1 < s2)
return false;
else
return true;
}
//---------------------------------------------------------------------------------------------------------
// logic comparision, boolean return type
bool operator==(const MyString& s1, const MyString& s2)
{
for(int i = 0; i < s1.stringlength; i++)
{
if(s1.stringentry[i] != s2.stringentry[i])
return false;
}
return true;
}
//----------------------------------------------------------------------------------------------------------
// logic comparision, boolean return type
bool operator!=(const MyString& s1, const MyString& s2)
{
if(s1 == s2)
return false;
else
return true;
}
//----------------------------------------------------------------------------------------------------------
MyString::MyString() // empty string
{
stringlength = 1;
stringentry = new char[stringlength];
*stringentry = '';
}
//----------------------------------------------------------------------------------------------------------
MyString::MyString(const char* entry) // conversion from c-string
{
stringlength = strlen(entry) + 1;
stringentry = new char[stringlength];
for( int i = 0; i < stringlength; i ++)
{
stringentry[i] = entry[i];
}
stringentry[stringlength - 1] = '';
}
//----------------------------------------------------------------------------------------------------------
MyString::MyString(int entry) // conversion from int
{
int entry1 = entry;
int counter = 0;
do // this loop calculates how many digits does entry have
{
entry1 = entry1 / 10;
counter++;
}while(entry1 != 0);
stringlength = counter + 1;
stringentry = new char[stringlength];
do // this loop puts individual digits to corresponding c-string indexes
{
stringentry[counter - 1] = char((entry % 10) + 48);
entry = entry / 10;
counter--;
}while(counter >= 1);
stringentry[stringlength - 1] = '';
}
//----------------------------------------------------------------------------------------------------------
MyString::~MyString() // destructor
{
delete [] stringentry;
}
//----------------------------------------------------------------------------------------------------------
MyString::MyString(const MyString& s) // copy constructor
{
stringlength = s.stringlength;
stringentry = new char[stringlength];
for(int i = 0; i < stringlength; i++)
{
stringentry[i] = s.stringentry[i];
}
}
//----------------------------------------------------------------------------------------------------------
MyString& MyString::operator=(const MyString& s) // assignment operator
{
if(*this != s)
{
delete [] stringentry;
stringlength = s.stringlength;
stringentry = new char[s.stringlength];
for(int i = 0; i < stringlength - 1; i++)
{
stringentry[i] = s.stringentry[i];
}
stringentry[stringlength - 1] = '';
}
return *this;
}
//----------------------------------------------------------------------------------------------------------
MyString& MyString::operator+=(const MyString& s) // concatenation/assignment
{
// stringtemp stores the c-string after concatenation
char* stringtemp = new char[stringlength + s.stringlength - 1];
for(int i = 0; i < stringlength - 1; i++)
{
stringtemp[i] = stringentry[i];
}
for(int i = 0; i < stringlength; i++)
{
stringtemp[i + stringlength - 1] = s.stringentry[i];
}
stringlength = stringlength + s.stringlength;
delete [] stringentry;
stringentry = stringtemp; // resultant object replaces calling object
return *this;
}
//----------------------------------------------------------------------------------------------------------
// bracket operators to access char positions
char& MyString::operator[] (unsigned int index)
{
if(index <= stringlength - 2) // in case index within boundary
return stringentry[index];
else // in case index out of boundary
{
char* tempstring = new char[index + 2];
for(int i = 0; i < stringlength - 1; i++)
tempstring[i] = stringentry[i];
for(int i = stringlength - 1; i < index + 1; i++)
tempstring[i] = ' ';
tempstring[index + 1] = '';
delete [] stringentry;
stringentry = tempstring;
stringlength = index + 2;
return stringentry[index];
}
}
//----------------------------------------------------------------------------------------------------------
// right-value bracket operator
const char& MyString::operator[] (unsigned int index) const
{
if(index <= stringlength - 2)
return stringentry[index];
else
return stringentry[stringlength - 1];
}
//----------------------------------------------------------------------------------------------------------
// insert s into the string at position "index"
MyString& MyString::insert(unsigned int index, const MyString& s)
{
if(index <= stringlength - 1) // if insertion happens within c-string of calling object
{
char* tempstring = new char[stringlength + s.stringlength - 1];
for(int i = 0; i < index; i++)
tempstring[i] = stringentry[i];
for(int i = 0; i < s.stringlength - 1; i++)
tempstring[i + index] = s.stringentry[i];
for(int i = index + s.stringlength - 1; i < stringlength + s.stringlength -2; i++)
tempstring[i] = stringentry[i - s.stringlength +1];
tempstring[stringlength + s.stringlength -2] = '';
delete [] stringentry;
stringentry = tempstring;
stringlength = stringlength + s.stringlength - 1;
}
else // if insertion is out of boundary, spaces replace unclaimed slots
{
char* tempstring = new char[stringlength + s.stringlength - 1];
for(int i = 0; i < stringlength - 1; i++)
tempstring[i] = stringentry[i];
for(int i = 0; i < s.stringlength - 1; i++)
tempstring[stringlength - 1 + i] = s.stringentry[i];
tempstring[stringlength + s.stringlength - 2] = '';
delete [] stringentry;
stringentry = tempstring;
stringlength = stringlength + s.stringlength - 1;
}
return *this;
}
//---------------------------------------------------------------------------------------------------------
// find index of the first occurrence of s inside the string
// return the index, or -1 if not found
int MyString::indexOf(const MyString& s) const
{
if(stringlength < s.stringlength) // object being called is longer than calling object
return -1;
else if(stringlength == s.stringlength) // object being called has same length with calling object
{
int i = 0;
while((stringentry[i] == s.stringentry[i]) && (stringentry[i] != ''))
i++;
if(stringentry[i] == '')
return 0;
else
return -1;
}
else // calling object is longer
{
int index = 0;
while(index < (stringlength - s.stringlength + 1))
{
int j = 0;
int k = 0;
while((s[0] != stringentry[index]) && (index < (stringlength - s.stringlength + 1)))
index++;
if(index == stringlength - s.stringlength + 1)
return -1;
else
{
k = index;
while((s[j] == stringentry[k]) && (s[j] != ''))
{
k++;
j++;
}
if(s[j] == '')
return index;
else
index++;
}
}
}
}
//----------------------------------------------------------------------------------------------------------
int MyString::getLength() const // return string length: without '' being counted
{
return stringlength - 1;
}
//----------------------------------------------------------------------------------------------------------
const char* MyString::getCString() const // return c-string equiv
{
return stringentry;
}
//----------------------------------------------------------------------------------------------------------
// given index and length of string of inquiry, returns its string object
MyString MyString::substring(unsigned int index, unsigned int length) const
{
MyString sub = substring(index);
if(sub.stringlength - 1 <= length)
{
return sub;
}
else
{
char* tempstring = new char[length + 1];
for(int i = 0; i < length; i++)
{
tempstring[i] = sub.stringentry[i];
}
tempstring[length] = '';
delete [] sub.stringentry;
sub.stringentry = tempstring;
sub.stringlength = length + 1;
return sub;
}
}
//-----------------------------------------------------------------------------------------------------------
// a shorter version of previous funtion
MyString MyString::substring(unsigned int index) const
{
MyString sub;
int tempsize = stringlength - index;
if(tempsize <= 1)
{
sub.stringentry = '';
sub.stringlength = 1;
}
else
{
char* tempstring = new char[tempsize];
for(int i = 0; i < tempsize - 1; i++)
{
tempstring[i] = stringentry[i + index];
}
tempstring[tempsize - 1] = '';
delete [] sub.stringentry;
sub.stringentry = tempstring;
sub.stringlength = tempsize;
}
return sub;
}
//-------------------------------------end of funtional definations--------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.