Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6378719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:02:40+00:00 2026-05-25T02:02:40+00:00

I have homework that I need to make a custom string class using C

  • 0

I have homework that I need to make a custom string class using C type strings. Most of it seems okay but I am getting a runtime error as soon as I test it and it isn’t performing all my tests like it should. Specifically my += operator seems to have something wrong with it but I can’t understand what.

I can’t add in or change any of the prototypes and I should use C++ constructs over C type where possible.

Thanks!

#include <iostream>
#include <string>
#include "tstr.h"
using namespace std;

//Default constructor to initialize the string to null
TStr::TStr() {
    strPtr = 0;
    strSize = 0;
}  
//constructor; conversion from the char string
TStr::TStr(const char *str) {
    int i=0;
    while (str[i] != '\0') {
        ++i;
    }
    strSize = i;
    strPtr = new char [i+1];
    for (i=0; i < strSize; ++i) {
        strPtr[i] = str[i];
    }
} 
//Copy constructor
TStr::TStr(const TStr& str) {
    strPtr = new char[str.strSize];
    strcpy(strPtr, str.strPtr);
}
//Destructor
TStr::~TStr() {
    delete[] strPtr;
}

//subscript operators-checks for range
char& TStr::operator [] (int i) {
    assert (i >= 0 && i < strSize);
    return strPtr[i];
}
const char& TStr::operator [] (int i) const {
    assert (i >= 0 && i < strSize);
    return strPtr[i];
}

//overload the concatenation oprerator
TStr TStr::operator += (const TStr& str) {
    char *buffer = new char[strSize + str.strSize + 1];
    strcpy(buffer, strPtr);
    strcat(buffer, str.strPtr);
    delete [] strPtr;
    strPtr = buffer;
    return *this;
}

//overload the assignment operator
const TStr& TStr::operator = (const TStr& str) {
    if (this != &str) {
        delete[] strPtr;
        strPtr = new char[strSize = str.strSize];
        assert(strPtr);
        for (int i=0; i<strSize; ++i) {
            strPtr[i] = str.strPtr[i];
        }
    }
    return *this;
}

//overload two relational operators as member functions
bool TStr::operator == (const TStr& str) const {
    int value = strcmp(strPtr, str.strPtr);
    if (value == 0) {
        return true;
    } else {
        return false;
    }
    /*int counter=0;
    for (int i=0; i < strSize; ++i) {
        if (strPtr[i] == str.strPtr[i]) {
            ++counter;
        }
    }
    if (counter == strSize) {
        return true;
    } else {
        return false;
    }
    return (strPtr == str.strPtr && strSize == str.strSize);*/
}
bool TStr::operator < (const TStr& str) const {
    return (strPtr < str.strPtr && strSize < str.strSize);
}
//the length of the string
int TStr::size() {
    return strSize;
}

//Overload the stream insertion and extraction operators.
ostream& operator << (ostream& out, const TStr& str) {
int size = str.strSize;
for (int i=0; i < size; ++i) {
    out << str[i];
}
return out;
}
istream& operator >> (istream& in, TStr& str) {
return in;
}

//overload two other relational operators as global functions
bool operator != (const TStr& S1, const TStr& S2) {
    return !(S1 == S2);
}
bool operator <= (const TStr& S1, const TStr& S2) {
    return (S1 < S2 || S1 == S2);
}
bool operator > (const TStr& S1, const TStr& S2) {
    return !(S1 < S2);
}
bool operator >= (const TStr& S1, const TStr& S2) {
    return !(S1 < S2 || S1 == S2);
}

//overload the concatenation operator as a global function
TStr operator + (const TStr& str1, const TStr& str2) {
    //return (str1 += str2);
    //return (str1 + str2);
}

This is what I’m testing with:

int main() {
authors();
TStr str1 = "VENI";         //initialize str1 using 
                                //the assignment   operator 
const TStr str2("VEDI");    //initialize str2 using the
                                    //conversion constructor
TStr str3; //initialize str3 to null
TStr str4; //initialize str4 to null

cout << "\nTest 1: str1: " << str1 << " str2 " << str2 
     << " str3 " << str3 << " ###.\n" ;          //Test 1

if (str1 <= str2)                               //Test 2  
    cout << "\nTest 2: " << str1 << " is less "
         << "than " << str2 << endl;            
else                                            
    cout << "\nTest 2: " << str2 << " is less "
         << "than " << str1 << endl;            

str1=" Pride is what we have.";

str3 = str1;                                    //Test 3
cout << "\nTest 3: The new value of str3 = "
     << str3 << endl;                          


str3 += " Vanity is what others have. ";              
cout<<"\nTest 4: The str3: '" << str3<<"'  \nhas ";        //test 4
cout<< countVowels(str3)<<" vowels "<< endl;

/*TStr str5 = str1 + str2 + str3;
cout<<"\nTest 5: The str5: '" << str5<<"' \nhas ";        //test 5
cout<<countVowels(str5)<<" vowels \n";

cout<<"\nTest 6: The str3 again: " << str3 <<endl;    //test 6

cout<<"\n\n Bye, Bye!";*/


return 0;
}

EDIT: I should have added that I need to get the tests to work as part of the homework.

EDIT 2: Here are the functions I changed so far.

//constructor; conversion from the char string
TStr::TStr(const char *str) {
    int i=0;
    while (str[i] != '\0') {
        ++i;
    }
    strSize = i;
    strPtr = new char [i+1];
    for (i=0; i < strSize; ++i) {
        strPtr[i] = str[i];
        strPtr[i + 1] = '\0'; //<- this
    }
} 
//Copy constructor
TStr::TStr(const TStr& str) {
    strPtr = new char[str.strSize + 1]; //<-this
    strcpy(strPtr, str.strPtr);
}
//overload the concatenation oprerator
TStr TStr::operator += (const TStr& str) {
    char *buffer = new char[strSize + str.strSize + 1]; //<- this
    strcpy(buffer, strPtr);
    strcat(buffer, str.strPtr);
    delete [] strPtr;
    strPtr = buffer;
    return *this;
}

//overload the assignment operator
const TStr& TStr::operator = (const TStr& str) {
    if (this != &str) {
        delete[] strPtr;
        strPtr = new char[strSize = str.strSize + 1]; //<- this
        assert(strPtr);
        for (int i=0; i<strSize; ++i) {
            strPtr[i] = str.strPtr[i];
        }
    }
    return *this;
}

EDIT 3: Unfortunately changing it to below did nothing really. It might be the = operator that’s causing the problem as it now fails just after test 3.

//constructor; conversion from the char string
TStr::TStr(const char *str) {
    int i=0;
    while (str[i] != '\0') {
        ++i;
    }
    strSize = i;
    strPtr = new char [i+1];
    strcpy(strPtr, str);
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:02:41+00:00Added an answer on May 25, 2026 at 2:02 am

    You have several problems involving the null-terminator with several of your functions(some of these you have fixed in your edits, but I’m just going to cover them all anyway, so that this is a thorough answer).

    1. In the constructor converting from const char *, you are not copying over the null-terminator to strPtr. (fixed)
    2. In your copy constructor, you are not allocating enough room for the null-terminator. (fixed)
    3. In your assignment operator, you are not allocating enough room for the null-terminator. (fixed … not exactly, see #5)
    4. In your assignment operator, you are not copying over the null-terminator.
    5. You added a new problem in your assignment operator when you fixed the allocation. You are now setting strSize to the wrong size.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.