fork-commit-merge / fork-commit-merge

Fork, Commit, Merge. A project designed to help you familiarize yourself with the open source contribution workflow on GitHub!

Home Page:https://forkcommitmerge.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fork, Commit, Merge - Hard Issue (C++)

nikohoffren opened this issue · comments

Fork, Commit, Merge - Hard Issue (C++)

Implement a Custom String Class with Basic Functionalities in C++

Note: You don't have ask permission to start solving the issue or get assigned, since these issues are supposed to be always open for new contributors. The actions-user bot will reset the file back to previous state for the next contributor after your commit is merged. So you can just simply start working with the issue right away!

Navigate to the tasks/c++/hard directory from the root of your project.
You will see three files: main.cpp, mystring.cpp and mystring.h.
You only need to modify mystring.cpp and mystring.h files for this task.

Task

Your objective is to create a basic custom string class named MyString with the following functionalities:

Constructors:

  • Default constructor.
  • Parameterized constructor to initialize from a given C-style string (character array).
  • Copy constructor.

Destructor.

Member functions:

  • length(): Return the length of the string.
  • c_str(): Return the underlying C-style string (character array).
  • append(MyString&): Append a given MyString object to the current string.
  • compare(MyString&): Compare the current string with a given MyString object. Return 0 if they're the same, a negative number if the current string is lexicographically smaller, and a positive number otherwise.

Overloaded operators:

  • Overload the assignment = operator for assignment of one MyString to another.
  • Overload the addition + operator to concatenate two MyString objects.
  • Overload the equality == operator to compare two MyString objects for equality.
  • Overload the stream insertion << operator for easy printing of the MyString object.

Requirements

  • Use dynamic memory allocation (e.g., new and delete) for string storage.
  • Handle memory leaks and ensure proper memory management.
  • The class should be robust against possible errors, such as null pointers.

Bonus

  • Implement a substring(int start, int end) function that returns a new MyString object containing characters from start to end.

Tips

  • Remember to check for null pointers before any operation.
  • During memory allocation, always check if memory was successfully allocated.
  • For string manipulation, you might find the C++ string functions like strcpy, strcat, etc., from cstring useful.

To test the program you have to first make sure you are in the right directory:

cd tasks/c++/hard

Then compile the files:

g++ main.cpp mystring.cpp -o mystring_test

Then you can run the compiled program by running:

./mystring_test

Running the program should output:

Hello World
Both strings are equal!

If the program works as requested in the task, you are ready to make a pull request!


To work with this issue, you need to have C++ installed to your local machine.
Check out README.md for more instructions of installing C++ and how to make a pull request.

Feel free to ask any questions here if you have some problems!

Also, kindly give this project a star to enhance its visibility for new developers!