Skip to content

Instantly share code, notes, and snippets.

@montreal91
Last active June 9, 2017 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montreal91/6a4289458c5cc5801170755c33741464 to your computer and use it in GitHub Desktop.
Save montreal91/6a4289458c5cc5801170755c33741464 to your computer and use it in GitHub Desktop.
Self-Made Strings
#include "string.h"
String::String()
{
m_characters = new char[0];
m_size = 0;
}
String::String( const int size )
{
m_size = size;
m_characters = new char[size];
}
String::String( const char* str )
{
m_size = 0;
int i = 0;
while ( str[i] )
{
m_size++;
i++;
}
m_characters = new char[m_size];
for (int i = 0; i < m_size; i++)
m_characters[i] = str[i];
}
String::String( const String& string )
{
m_size = string.m_size;
m_characters = new char[m_size];
for ( int i = 0; i < m_size; i++ )
m_characters[i] = string.m_characters[i];
}
String::~String()
{
delete [] m_characters;
}
char& String::operator []( int index )
{
return m_characters[index];
}
const char String::operator []( int index ) const
{
return m_characters[index];
}
String& String::operator=( const String& string )
{
if ( this != &string )
{
m_size = string.m_size;
m_characters = new char[m_size];
for (int i = 0; i < m_size; i++ )
m_characters[i] = string.m_characters[i];
}
return *this;
}
void String::Print( std::ostream& os ) const
{
for ( int i = 0; i < m_size; i++ )
os << m_characters[i];
os << "\n";
}
int String::GetSize() const
{
return m_size;
}
char ToUpper( char ch )
{
if ( islower( ch ) )
return ch - static_cast<char>( 32 );
else
return ch;
}
bool operator > ( const String& lstr, const String& rstr )
{
int size = std::min( lstr.GetSize(), rstr.GetSize() );
for ( int i = 0; i < size; i++ )
{
if ( ToUpper( lstr[i] ) > ToUpper( rstr[i] ) )
return true;
else if ( ToUpper( lstr[i] ) < ToUpper( rstr[i] ) )
return false;
}
return lstr.GetSize() > rstr.GetSize();
}
bool operator >= ( const String& lstr, const String& rstr )
{
int size = std::min( lstr.GetSize(), rstr.GetSize() );
for ( int i = 0; i < size; i++ )
{
if ( ToUpper( lstr[i] ) >= ToUpper( rstr[i] ) )
return true;
else if ( ToUpper( lstr[i] ) < ToUpper( rstr[i] ) )
return false;
}
return lstr.GetSize() >= rstr.GetSize();
}
bool operator < ( const String& lstr, const String& rstr )
{
int size = std::min( lstr.GetSize(), rstr.GetSize() );
for ( int i = 0; i < size; i++ )
{
if ( ToUpper( lstr[i] ) < ToUpper( rstr[i] ) )
return true;
else if ( ToUpper( lstr[i] ) > ToUpper( rstr[i] ) )
return false;
}
return lstr.GetSize() < rstr.GetSize();
}
bool operator <= ( const String& lstr, const String& rstr )
{
int size = std::min( lstr.GetSize(), rstr.GetSize() );
for ( int i = 0; i < size; i++ )
{
if ( ToUpper( lstr[i] ) <= ToUpper( rstr[i] ) )
return true;
else if ( ToUpper( lstr[i] ) > ToUpper( rstr[i] ) )
return false;
}
return lstr.GetSize() <= rstr.GetSize();
}
#ifndef __STRING_H_
#define __STRING_H_
#include <algorithm>
#include <ctype.h>
#include <iostream>
#include "heap.h"
namespace tg
{
class String
{
public:
String();
String( const int size );
String( const char* str );
String( const String& string );
~String();
char& operator []( int index );
const char operator []( int index ) const;
String& operator=( const String& string );
void Print( std::ostream& os ) const;
int GetSize() const;
private:
char *m_characters;
int m_size;
};
char ToUpper( char letter );
bool operator > ( const String& lstr, const String& rlsr );
bool operator >= ( const String& lstr, const String& rlsr );
bool operator < ( const String& lstr, const String& rlsr );
bool operator <= ( const String& lstr, const String& rlsr );
#endif // __STRING_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment