Gestion de chaine de caractére en c++ avec nstring

Contenu du snippet

voila une Classe qui gère les Chaine de caractère. cette version est vraiment pauvre mais toute aide ou une suggestion sera la bienvenue
merci

Source / Exemple :


// dans le Header *.h 

#include <iostream>

/*     NString  version 1.0 
    
  Web: http://www.niceclub.hostzi.com

  • /
class NString { public: NString(); NString(const NString &Str); NString(const char * Str); ~NString(); int StrCount(); NString operator=(const char *Str); NString operator=(const NString &Str); NString operator+(const char *Str); NString operator+(const NString &Str); char * c_str(); bool IsEmpty(); void Clear(); private: char *m_str; int m_Count; }; // Dans le CPP #include "NString.h" /* NString version 1.0 Web: http://www.niceclub.hostzi.com
  • /
int NStrLen(const char * Str) { int i = 0; while ( Str[i] != '\0' ) { i++; } return i; } char * copy(const char * str) { int Len = NStrLen(str); //int Len = strlen(str); char * StrCopy = new char[Len + 1]; for (int i = 0; i < Len ; i++ ) { StrCopy[i] = str[i]; } StrCopy[Len] = '\0'; return StrCopy; } NString::NString() { this->m_Count = 0; this->m_str = NULL; } NString::NString( const NString &Str ) { m_str = copy(Str.m_str); m_Count = Str.m_Count; } NString::NString( const char * Str ) { m_str = copy(Str); m_Count = NStrLen(Str); } int NString::StrCount() { return m_Count; } NString::~NString() { delete[] m_str; } NString NString::operator=( const char * Str ) { delete[] m_str; m_str = copy(Str); m_Count = NStrLen(Str); return *this; } NString NString::operator=( const NString &Str ) { delete[] m_str; m_str = copy(Str.m_str); m_Count = Str.m_Count; return *this; } NString NString::operator+( const char * Str ) { int SizeAll = m_Count + NStrLen(Str); char * End = new char[SizeAll + 1]; for (int i = 0; i < NStrLen(m_str); i++ ) { End[i] = m_str[i]; } for (int i = m_Count; i < SizeAll; i++ ) { End[i] = Str[ i - m_Count ]; } End[SizeAll] = '\0'; NString sRet(End); delete [] End; return sRet; } NString NString::operator+( const NString &Str ) { int SizeAll = m_Count + Str.m_Count; char * End = new char[SizeAll + 1]; for (int i = 0; i < NStrLen(m_str) ; i++ ) { End[i] = m_str[i]; } for (int i = m_Count; i < SizeAll; i++ ) { End[i] = Str.m_str[ i - m_Count]; } End[SizeAll] = '\0'; NString sRet(End); delete[] End; return sRet; } char * NString::c_str() { return m_str; } bool NString::IsEmpty() { if (m_Count == 0 ) { return true; } return false; } void NString::Clear() { delete[] m_str; m_Count = 0; }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.