Question: Complete the String class (note the capital S !) that defines strings. Your completed String class should make the given main testing function work as
Complete the String class (note the capital S !) that defines strings. Your completed String class should make the given main testing function work as required. You can download the main function from Moodle. Output from my implementation is also attached in the testing file for your reference.
Coding Requirements:
1. Do not include
2. You are not allowed to use any cstring library functions.
3. Your String class must have the following two, and exactly two member variables. You need to make use of the variable len in your implementation effectively. To facilitate marking, please use the exact variable names str and len as given below. Also note their meanings explained as follows:
private:
char* str; // null character terminated character array allocated dynamically
int len; // the actual length of 'this' string,
// e.g. if s1 is "hello", then the member variable 'len' of s1 is 5.
4. You must include the destructor implemented as follows:
String::~String() { delete [] str; }
5. The indexing operator must be implemented as follows:
char& String::operator[] (int index) const
{
if (index < 0 || index >= len)
throw "index out of bounds";
return str[index];
}
6. A binary operator whose operands are both of String type must be implemented as a non-member operator (except for the assignment operator). Whether they are friend or non-friend is your choice. In all other cases, the operator should be implemented as a member.
7. Do not implement any non-member function (not operator) as a friend of String.
8. Use const keyword for object arguments, array arguments, and member functions wherever applicable. Do not use const for primitive type parameters.
9. Wherever a dynamic variable/array is not needed any more, clean it up (delete it) before you move away the pointer initially pointing to it.
10. Once you complete your String class implementation, you need to create more test cases to test your class to make sure it is fully functional. When I mark your assignment, I may use a different and more thorough testing code than the given one to test your class implementation.
11. Do not use 'this' operator
//main function from Moodle for testing code
int main()
{
String s1, s2;
cout << "s1 is empty? " << s1.empty() << endl;
cout << "s2 is empty? " << s2.empty() << endl;
String s3 = "Hello";
s1 = s2 = s3;
cout << s1 << '\t' << s2 << '\t' << s3 << endl;
if (s1 == s2)
cout << "s1 and s2 are equal ";
else
cout << "s1 and s2 are not equal ";
s1 = s1;
cout << " Is there any problem here? " << s1 << endl << endl;
s2 = "hi";
s3 = s2;
cout << s1 << '\t' << s2 << '\t' << s3 << endl;
cout << "Now s1 is empty? " << s1.empty() << endl;
cout << s1.length() << '\t' << s2.length() << '\t' << s3.length() << endl;
cout << "--------------- 0 ---------------" << endl << endl;
if (s1 == s2)
cout << "They are equal ";
else
cout << "They are not equal ";
if (s2 == "Hello")
cout << "They are equal ";
else
cout << "They are not equal ";
s1[0] = 'H';
s1[1] = s1[4];
for(int i = 0; i < s3.length(); ++i)
cout << s3[i] << " ";
cout << endl;
cout << "s1 is " << s1 << endl;
cout << "s2 is " << s2 << endl;
cout << "--------------- 1 ---------------" << endl << endl;
String s4;
String space (" ");
s4 = s1 + space + s2;
cout << s4 << endl;
cout << "s4 length is " << s4.length() << endl;
if (s4 == s4)
cout << "s4 equals s4 ";
else
cout << "s4 not equals s4 ";
cout << endl;
s3 = "Monday";
cout << "s3 is " << s3 << endl;
s3 = "";
cout << "s3 is " << s3 << endl;
String friday = "Friday";
s3 = s3 + friday;
cout << "s3 is " << s3 << endl;
s3 = "SundaySunday";
s3 = s3 + " hahaha";
cout << "s3 is " << s3 << endl;
cout << "--------------- 2 ---------------" << endl << endl;
String s5;
cout << "Do we have output here? " << endl << endl;
for(int i = 0; i < s5.length(); ++i)
cout << s5[i] << '?';
cout << "s4 is " << s4 << endl;
cout << s4.upper() << endl;
cout << s4 << endl;
cout << "The index of l in " << s4 << " is " << s4.position('l') << endl;
cout << "The index of k in " << s4 << " is " << s4.position('k') << endl;
cout << "--------------- 3 ---------------" << endl << endl;
cout << "s1 is: " << s1 << endl;
char char_o = 'o';
s1 += char_o;
String str_o = "o";
s1 += str_o;
cout << "s1 now is: " << s1 << endl;
String str;
cout << "str is " << str << endl;
char ch = 'A';
for(int i = 0; i < 10; ++i)
{
str += ch;
ch++;
}
String more = "...MORE";
str += more;
cout << "Now str is " << str << endl;
cout << "--------------- 4 ---------------" << endl << endl;
cout << "str length: " << str.length() << endl;
cout << "substring starting at 2 with size 5 is " << str.substr(2,5) << endl;
cout << "substring starting at 2 with size 20 is " << str.substr(2,20) << endl;
cout << "substring starting at 7 with size 1 is " << str.substr(7,1) << endl;
cout << "substring starting at 20 with size 5 is " << str.substr(20,5) << "empty string!" << endl;
cout << "--------------- 5 ---------------" << endl << endl;
s3 = "Hi";
int index = s1.substring(s3);
if (index >= 0)
cout << "1) " << s3 << " is a substring of " << s1 << ", starting from index " << index << endl;
else
cout << "1) " << s3 << " is not a substring of " << s1 << endl;
index = s1.substring("lo");
if (index >= 0)
cout << "2) " << "lo is a substring of " << s1 << ", starting from index " << index << endl;
else
cout << "2) " << "lo is not a substring of " << s1 << endl << endl;
String s6;
index = s1.substring(s6);
if (index >= 0)
cout << "3) " << s6 << " is a substring of " << s1 << ", starting from index " << index << endl;
else
cout << "3) " << s6 << " is not a substring of " << s1 << endl;
String s = "aabcccdefefg";
index = s.substring("bdg");
if (index >= 0)
cout << "4) bdg is a substring of " << s << ", starting from index " << index << endl;
else
cout << "4) bdg is not a substring of " << s << endl;
index = s.substring("cde");
if (index >= 0)
cout << "5) cde is a substring of " << s << ", starting from index " << index << endl;
else
cout << "5) cde is not a substring of " << s << endl;
s = "aabc";
s1 = "aabc ";
index = s.substring(s1);
if (index >= 0)
cout << "6) " << s1 << " is a substring of " << s << ", starting from index " << index << endl;
else
cout << "6) " << s1 << " is not a substring of " << s << endl;
index = s.substring(s);
if (index >= 0)
cout << "7) " << s << " is a substring of " << s << ", starting from index " << index << endl;
else
cout << "7) " << s << " is not a substring of " << s << endl;
cout << "--------------- 6 ---------------" << endl << endl;
s1 = "hello";
s2 = "hi";
s3 = "he";
s4 = "hello";
s5 = "";
cout << compareStrings (s1, s2) << endl; // the behavior of this function is the same as strcmp
cout << compareStrings (s1, s3) << endl;
cout << compareStrings (s1, s4) << endl;
cout << compareStrings (s5, s2) << endl;
cout << compareStrings (s2, s5) << endl;
cout << compareStrings (s1, s1) << endl << endl;
cout << s1 << endl;
String reversed = reversedString (s1);
cout << "The reversed string is " << reversed << endl;
cout << "The original string is " << s1 << endl;
if (reversedString (s1) == reversedString (s4))
cout << "Yes, equal!" << endl;
else
cout << "No, not equal!" << endl;
system("pause"); return 0;
}
/* Output for String testing code:
s1 is empty? 1
s2 is empty? 1
Hello Hello Hello
s1 and s2 are equal
Is there any problem here? Hello
Hello hi hi
Now s1 is empty? 0
5 2 2
--------------- 0 ---------------
They are not equal
They are not equal
h i
s1 is Hollo
s2 is hi
--------------- 1 ---------------
Hollo hi
s4 length is 8
s4 equals s4
s3 is Monday
s3 is
s3 is Friday
s3 is SundaySunday hahaha
--------------- 2 ---------------
Do we have output here?
s4 is Hollo hi
HOLLO HI
Hollo hi
The index of l in Hollo hi is 2
The index of k in Hollo hi is -1
--------------- 3 ---------------
s1 is: Hollo
s1 now is: Hollooo
str is
Now str is ABCDEFGHIJ...MORE
--------------- 4 ---------------
str length: 17
substring starting at 2 with size 5 is CDEFG
substring starting at 2 with size 20 is CDEFGHIJ...MORE
substring starting at 7 with size 1 is H
substring starting at 20 with size 5 is empty string!
--------------- 5 ---------------
1) Hi is not a substring of Hollooo
2) lo is a substring of Hollooo, starting from index 3
3) is a substring of Hollooo, starting from index 0
4) bdg is not a substring of aabcccdefefg
5) cde is a substring of aabcccdefefg, starting from index 5
6) aabc is not a substring of aabc
7) aabc is a substring of aabc, starting from index 0
--------------- 6 ---------------
-1
1
0
-1
1
0
hello
The reversed string is olleh
The original string is hello
Yes, equal!
*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
