Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
Last active January 5, 2017 00:29
Show Gist options
  • Save SeijiEmery/b29eeea63f428e90450f to your computer and use it in GitHub Desktop.
Save SeijiEmery/b29eeea63f428e90450f to your computer and use it in GitHub Desktop.
String implementation challenge
// C++ challenge: implement a string class - specifically this one, which provides most
// of basic string operations you'd need to interface w/ other C++ code.
// Note: using the stl is cheating (obviously), as much of the challenge here is in
// learning how to do low level memory management and pointer arithmetic.
class String {
public:
String (); // Default constructor (construct empty string)
String (const char *c_str); // Construct a String type from a c (null terminated) string
String (const String &); // Copy constructor
String (String &&); // Move constructor (if you're ambitious)
virtual ~String (); // Destructor (should be virtual)
size_t length () const; // Get the string's length (could alt. be named 'size')
void append (const String & other); // Append another string onto this one
const char * c_str () const; // Get internal string representation / return a c-string
// Provide functionality to write the string to an ostream (like std::cout)
// Note: A friend function is a normal function that has permission to view this class's
// internal contents (like a method), but is external to the class.
// Can be defined inline for simplicity.
friend std::ostream & operator << (std::ostream & os, const String & string);
// Provide an operator += overload, which should function the same as append. (This actually
// is a method, and should return *this so that the operation can be chained)
String & operator += (const String & other);
// Provide an operator that produces a *new* string by concatenating this and another one.
// Shouldn't modify itself (hence const).
String operator + (const String & other) const;
// Access individual characters within the string
char operator[] (size_t index) const;
protected:
// Implement the internals yourself
// (hint: should include a dynamic array if you want a mutable string (ie. can append stuff to it))
};
// C challenge: produce implementations for the following functions:
size_t strlen (const char *str);
void * memcpy (void *dst, const void *src, size_t num_bytes);
char * strcat (char *dst, const char *src);
char * strncat (char *dst, const char *src, size_t max_buffer_size);
char * strcpy (char *dst, const char *src);
char * strncpy (char *dst, const char *src, size_t max_buffer_size);
// Not actually that difficult, and it's good way to learn/master pointer arithmetic.
// (The other challenges I'd recommend are linked lists, binary trees (any variation), and
// possibly writing a dynamic array (ie. vector) implementation – though there's a lot of
// similarities between the last one and implementing a string datatype (which is basically a
// specialized vector))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment