Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nikohoffren opened this issue Aug 20, 2023 · 0 comments
Closed

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

nikohoffren opened this issue Aug 20, 2023 · 0 comments

Comments

@nikohoffren
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant