Complete the String class (note the capital S !) that defines strings. Your comp
ID: 3597342 • Letter: C
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 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 <string>, you are not allowed to use any C++ string functions. We are defining our own string class.
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 << ' ' << s2 << ' ' << 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 << ' ' << s2 << ' ' << s3 << endl;
       cout << "Now s1 is empty? " << s1.empty() << endl;
       cout << s1.length() << ' ' << s2.length() << ' ' << 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;
}
Explanation / Answer
Given below is the implementation of the String class in .h and .cpp. The main test program uses 2 functions compareStrings() and reversedString(). I did not find their definition in the question posted. So I have implemented them in String.cpp file. If you already have those 2 functions , please don't use the definitions for compareStrings() and reversedString() from answer. Instead use your existing definitions.
If any issues, post a comment and I shall help. If the answer helped, please don't forget to rate the answer . Thank you.
String.h
#ifndef String_h
#define String_h
#include <iostream>
using namespace std;
class String
{
private:
char* str; // null character terminated character array allocated dynamically
int len; // the actual length of 'this' string,
public:
String();
String(char *s);
String(const String & s);
String & operator = (const String &s);
char& operator[] (int index) const;
bool empty() const;
int length() const;
String upper() const;
int position(char ch) const;
String operator +(const String &s2);
String substr(int start, int len);
int substring(const String & str2);
String& operator +=(const String &s2);
String& operator +=(char ch);
friend bool operator == (const String &s1, const String& s2);
friend ostream& operator << (ostream& out, const String &s);
 Â
~String();
};
//some string functions
int stringlen(char *s);
char toUpper(char ch);
int compareStrings(const String& s1, const String& s2);
String reversedString(const String& s1);
#endif /* String_h */
String.cpp
#include "String.h"
//some string functions
int stringlen(char *s)
{
int len = 0;
while(*s++ != '')
len++;
return len;
}
char toUpper(char ch)
{
if(ch >= 'a' && ch <= 'z')
return ch - 'a' + 'A';
else
return ch;
}
int compareStrings(const String& s1, const String& s2)
{
int i;
for(i = 0; i < s1.length() && i < s2.length() ; i++)
{
if(s1[i] < s2[i])
return - 1;
else if(s1[i] > s2[i])
return 1;
}
 Â
if(s1.length() == s2.length())
return 0;
else if(i == s1.length())
return -1;
else
return 1;
}
String reversedString(const String& s1)
{
String rev;
 Â
for(int i = s1.length() - 1; i >= 0; i--)
{
rev += s1[i];
}
 Â
return rev;
}
//--------------------------------------
String::String()
{
str = new char[1];
*str = '';
len = 0;
}
String::String (char* s)
{
len = stringlen(s);
str = new char[len+1];
for(int i = 0; i <= len; i++)
str[i] = s[i];
}
String::String(const String& s)
{
str = new char[s.len + 1];
for(int i = 0; i <= s.len; i++)
str[i] = s.str[i];
len = s.len;
 Â
}
String& String::operator = (const String &s)
{
 Â
delete[] str;
str = new char[s.len + 1];
for(int i = 0; i <= s.len; i++)
str[i] = s.str[i];
len = s.len;
return *this;
}
char& String::operator[] (int index) const
{
if (index < 0 || index >= len)
throw "index out of bounds";
return str[index];
}
bool String::empty() const
{
return len == 0;
}
int String::length() const
{
return len;
}
String String::upper() const
{
String upp;
delete[] upp.str;
upp.str = new char[len + 1];
for(int i= 0; i <= len; i++)
upp.str[i] =toUpper(str[i]);
upp.len = len;
return upp;
}
int String::position(char ch) const
{
for(int i = 0 ;i < len; i++)
if(str[i] == ch)
return i;
 Â
return -1;
}
ostream& operator << (ostream& out, const String &s)
{
out << s.str ;
return out;
}
String String::substr(int start, int length)
{
String sub;
delete []sub.str;
sub.str = new char[length + 1];
 Â
int i, j;
for(i = 0, j = start; j < len && i < length; i++, j++)
sub.str[i] = str[j];
sub.str[i] = '';
sub.len = i;
return sub;
}
int String::substring(const String & str2)
{
int last = len - str2.len;
for(int i = 0 ; i <= last; i++)
{
bool found = true;
for(int j = i, k = 0; k < str2.len; k++, j++)
{
if(str[j] != str2.str[k])
{
found = false;
break;
}
 Â
}
if(found)
return i;
 Â
}
return -1;
}
bool operator == (const String &s1, const String& s2)
{
if(s1.len != s2.len)
return false;
for(int i = 0; i < s1.len; i++)
{
if(s1.str[i] != s2.str[i])
return false;
}
return true;
}
String::~String()
{
delete [] str;
}
String String::operator +(const String &s2)
{
String res;
delete []res.str;
res.len =len + s2.len ;
res.str = new char (res.len + 1);
int i = 0;
for(int j = 0; j < len; j++)
res.str[i++] = str[j];
for(int j = 0; j < s2.len; j++)
res.str[i++] = s2.str[j];
res.str[i] = '';
return res;
 Â
}
String& String::operator +=(const String &s2)
{
*this = *this + s2;
return *this;
}
String& String::operator +=(char ch)
{
 Â
char *temp = new char[len+2]; //extra for ch and ''
for(int i = 0 ; i < len; i++)
temp[i] = str[i];
temp[len] = ch;
temp[len + 1] = '';
len++;
delete[] str;
str = temp;
return *this;
}
main.cpp
#include <iostream>
#include "String.h"
using namespace std;
//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 << ' ' << s2 << ' ' << 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 << ' ' << s2 << ' ' << s3 << endl;
cout << "Now s1 is empty? " << s1.empty() << endl;
cout << s1.length() << ' ' << s2.length() << ' ' << 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
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 hSundayÿÿ
--------------- 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!
sh: pause: command not found
Program ended with exit code: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.