// cString.cpp
// 24 de Outubro
// Versão 1, Implementação 2 com Sentinela

#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include "cString.h"


cString::cString()
{
	pStr = ( char *) malloc( sizeof(char) );
	*pStr = '\0';
}

cString::~cString()
{
	free( pStr );
}

int cString::Comprimento ()
{	
	return strlen( pStr );
}


void cString::Copiar( const char *s )
{

	free( pStr );

	// Com 2 Passos
	// pStr = ( char *) malloc( ( strlen(s) + 1 ) * sizeof(char) );
	// strcpy( pStr, s);
	
	// Só 1 Passo 
	pStr = strdup(s);
}


void cString::Concatenar( const char *s)
{
	int lenDest = strlen(pStr); 
	int lenSrc = strlen(s);

	pStr=(char *) realloc( pStr, ( lenDest + lenSrc + 1 ) * sizeof(char) );
	
	strcpy( pStr + lenDest, s);	
}

void cString::Escrever()
{
	cout << pStr;
}
