Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the provided String class to internally store Pascal strings, which are c

ID: 3689894 • Letter: M

Question

Modify the provided String class to internally store Pascal strings, which are character arrays that start with a number of characters in the string, followed by those characters, with NO terminating null character. That is:

str should contain a Pascal string.

The constructor created in your Pascal string lab should be included. The normal C-string constructor should also be included, and must convert to Pascal string.

The c_str function must convert back to a C string to provide a C string to the user. It is okay to allocate the memory for the string in this function.

All other functions must perform correctly given the change to the internal string format.

You may NOT store a C string internally along with the Pascal string.

Explanation / Answer

Pascal.H:

//#pragma once
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>

class String {
public:
// Usage: String aStringObj; or String aStringObj();
String();

// Constructor. Converts a char* object to a String object
// Usage: String aStringObj("hello"); or String aStringObj = "hello";
String(const char *s);

// Copy and move constructors.
// Usage: String aStringObject(anotherStringObj); or
// String aStringObject = anotherStringObj;
String(const String &s);
String(const char *str, bool pascal);
String(String &obj);

// Destructor
~String();

// Assignment operator
// Usage: aStringObject = anotherStringObj; or
// aStringObject.operator=(anotherStringObj);
String &operator=(const String &rhsObject);
String& operator=(String& rhs);

// Mutator operator[]
// Usage: aStringObject[1] = ’M’;
char &operator[] (int index);

// Accessor operator[]
// Usage: char ch = aStringObject[1];
char operator[] (int index) const;

// Get the length (number of characters) of a String object
// Usage: int len = aStringObject.Length();
;
int length() const;
int strlen(const char *str)const;

// Friend functions for == comparison
// Usage: if (aStringObject == anotherStringObj) {...} or
// if (aStringObject == "hello") {...} or
// if ("hello" == aStringObj) {...} or
friend bool operator==(const String &lhsObject, const String &rhsObject);

// The other five comparison operators
// !=, <, >, <=, >= are similarly handled as in line 13.

friend bool operator<(const String &lhsObject, const String &rhsObject);
friend bool operator>(const String &lhsObject, const String &rhsObject);
friend bool operator<=(const String &lhsObject, const String &rhsObject);
friend bool operator>=(const String &lhsObject, const String &rhsObject);
friend bool operator!=(const String &lhsObject, const String &rhsObject);

// Friend function for string concatenation
// Usage: StringOne = StringTwo + StringThree or
// StringOne = "hello" + StringTwo or
// StringOne = StringTwo + "hello"
friend String operator+(const String &lhs, const String &rhs);

// Return C style character string
// Usage: const char *str = aStringObj.C_str();
const char *c_str() const;

// Friend function for output
// Usage: cout << aStringObj;
friend std::ostream &operator<<(std::ostream &out, const String &obj);

private:
// arr implements the String object as a dynamic array
char *char_array;

// len keeps track of the length
int len;

// judge weather the String is a c_string or a pascal_string
bool judge;
};

#endif

Pascal.cpp:

// Pascal Main cpp


#include <iostream>
#include <iosfwd>
#include <algorithm>
#include "Pascal.h"
#include <exception>
using namespace std;
// Default constructor
String::String() {
char_array = new char[1];
char_array[0] = '';
len = 0;
}

// Constructor. Converts a C-string to a String object
String::String(const char *s) {
len = strlen(s);
char_array = new char[len + 1];
std::copy(s, s + len + 1, char_array);
}
// Get the length (number of characters) of a String object
// Usage: int len = aStringObject.Length();
int strlen(const char *str)
{
   int len = 0;
   while(str[len]!='')
   {
       len++;
       }
       return len;
   }
  
   int strcmp(const char *a,const char *b)
{
   int i=0;
   int l= strlen(a);
   while(a[i]==b[i])
   {
       i++;
   }
   if(l==i)
   return -1;
   else
   return 0;
  
}

// Copy constructor.
String::String(const String &obj) {
len = obj.len;
char_array = new char[len + 1];
std::copy(obj.char_array, obj.char_array + len + 1, char_array);
}

// Move constructor.
String::String(String &obj) {
len = obj.len;
char_array = obj.char_array;
obj.char_array = '';
}

String::String(const char *str, bool pascal) {
judge = pascal;
if (judge) {
len = strlen(str) - 1;
const char *temp = str;
char_array = new char[len + 1];
char_array[0] = len + '0';
for (int i = 1; i <= len; i++) {
char_array[i] = temp[i];
}

}
else {
len = strlen(str);
char_array = new char[len + 1];
std::copy(str, str + len + 1, char_array);
}
}

// Destructor
String::~String() {
if (char_array != '')
delete[] char_array;
}

// Assignment operator
String &String::operator=(const String &rhs) {
delete[] char_array;
len = rhs.len;
char_array = new char[len + 1];
std::copy(rhs.char_array, rhs.char_array + len + 1, char_array);
return *this;
}

// Move assignment operator
String &String::operator=(String &rhs) {
delete[] char_array;
len = rhs.len;
char_array = rhs.char_array;
rhs.char_array = '';
return *this;
}


// Mutator operator[]
char &String::operator[](int index) {
// check whether the index is within bounds
if (index > len || index < 0)
throw std::out_of_range("Index out of range");
return char_array[index];
}

// Accessor operator[]
char String::operator[](int index) const {
// check whether the index is within bounds
if (index > len || index < 0)
throw std::out_of_range("Index out of range");
return char_array[index];
}

// Get the length (number of characters) of a String object
int String::length() const {
return len;
}

bool operator==(const String &lhs, const String &rhs) {
if (lhs.judge != rhs.judge) {
cout << "can't compare";
}
return strcmp(lhs.char_array, rhs.char_array) == 0;
}

bool operator<(const String &lhs, const String &rhs) {
if (lhs.judge != rhs.judge) {
cout << "can't compare";
}
return strcmp(lhs.char_array, rhs.char_array) < 0;
}

// Friend functions for > comparison
bool operator>(const String &lhs, const String &rhs) {
if (lhs.judge != rhs.judge) {
cout << "can't compare";
}
return rhs < lhs;
}

// Friend functions for <= comparison
bool operator<=(const String &lhs, const String &rhs) {
if (lhs.judge != rhs.judge) {
cout << "can't compare";
}
return !(rhs < lhs);
}

// Friend functions for >= comparison
bool operator>=(const String &lhs, const String &rhs) {
if (lhs.judge != rhs.judge) {
cout << "can't compare";
}
return !(lhs < rhs);
}

// Friend functions for != comparison
bool operator!=(const String &lhs, const String &rhs) {
if (lhs.judge != rhs.judge) {
cout << "can't compare";
}
return !(lhs == rhs);
}

// Friend function for string concatination
String operator+(const String &lhs, const String &rhs) {
if (lhs.judge == rhs.judge && lhs.judge == false) {
int strLength = lhs.len + rhs.len + 1;
char *tmpStr = new char[strLength];
for (int i = 0; i < lhs.len; ++i)
tmpStr[i] = lhs.char_array[i];
for (int i = 0; i <= rhs.len; ++i)
tmpStr[lhs.len + i] = rhs.char_array[i];
String retStr(tmpStr);
delete[] tmpStr;
return retStr;
}
else if (lhs.judge == rhs.judge && lhs.judge == true) {
int strLength = lhs.len + rhs.len + 1;
char *tmp = new char[strLength];
for (int i = 1; i <= lhs.len; ++i)
tmp[i] = lhs.char_array[i];
for (int i = 1; i <= rhs.len; ++i)
tmp[lhs.len + i] = rhs.char_array[i];
tmp[0] = (lhs.len + rhs.len) + '0';
String retStr(tmp);
delete[] tmp;
return retStr;
}
else {
return String("can't do that");
}
}

// Return C style character string
const char* String::c_str() const {
return char_array;
}

// Friend function for output
std::ostream& operator<<(std::ostream &out, const String &obj) {
return out << obj.c_str();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote