A string in C is simply an array of characters with the null

A string in C++ is simply an array of characters with the null character(\\0)
used to mark the end of the string. C++ provides a set of string handling function in
<string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard
Template Library), C++ now provides a string class.

But for this assignment, you are to develop your own string class. This will give you a
chance to develop and work with a C++ class, define constructors and destructors, define
member functions outside of the class body, develop a copy constructor and assignment
operator (and understand why!), work with C-Style strings and pointers, dynamically
allocate memory and free it when done.

Of course, you must also do suitable testing which implies writing a main function that
uses your new string class.


The following is the skeleton of the Mystring class declaration. Mystring.h file is
provided. You must produce the Mystring.cpp and main.cpp files.

class Mystring
{
private:
char *pData; //pointer to simple C-style representation of the string
//(i.e., sequence of characters terminated by null)
//pData is only a pointer. You must allocate space for
//the actual character data
int length; //length of the string

//… //possibly other private data

public:
MyString(); //constructor --- create empty string
MyString(char *cString); //constructor --- create a string whose data is a copy of
//cString
~MyString(); //destructor -- don\'t forget to free space allocated by the constructor
//i.e., the space allocated for the character data

MyString(MyString const& s); //override the default copy constructor --- why?
//important -- think about it -- possible test question
MyString operator = (MyString const& s); //override default assignment operator
void Put(); //output string
void Reverse(); //reverse the string

MyString operator + (MyString const& s); //concatenation operator
// … //other useful member functions
//as you wish
};

In addition, prepare a graphical explanation of each of your member functions. (Pseudo
code or flowchart or some diagram to show your design.)

#ifndef MYSTRING_H
#define MYSTRING_H

class MyString
{
private:
char *pData; //pointer to simple C-style representation of the string
//(i.e., sequence of characters terminated by null)
//pData is only a pointer. You must allocate space for
//the actual character data
int length; //length of the string

// … //possible other private data

public:
MyString(); //constructor --- create empty string
MyString(char *cString); //constructor --- create a string whose data is a copy of
//cString
~MyString(); //destructor -- don\'t forget to free space allocated by the constructor
//i.e., the space allocated for the character data

MyString(MyString const& s); //override the default copy constructor --- why?
//important -- think about it -- possible test question
MyString operator = (MyString const& s); //override default assignment operator
void Put(); //output string
void Reverse(); //reverse the string

MyString operator + (MyString const& s); //concatenation operator
// … //other useful member functions
//as you wish
};

#endif // MYSTRING_H



Solution

mystring.h

#ifndef MY_STRING_H
#define MY_STRING_H

#include <iostream>

#include <string.h>   

using namespace std;
int my_strlen( char * ptr ){
int len = 0;
char * p = ptr;
while( *p != \'\\0\' ) {
len++;
p++;
}
return len;
}

void my_strncpy( char * ptr_dest, char * ptr_src, int n ){
for( int i=0; i < n; i++ )
ptr_dest[i] = ptr_src[i];
}

class myString {
  
public:

int length; // length of the string
char * buff; // pointer to strorage

  

myString();
~myString();
myString( char* cString );
myString( const myString& other );
int size();
myString& operator= ( const myString& other );
char& operator[] (int index );
myString operator+( const myString& s1 );
void Put();
void Reverse();
  
  
  

  

  
};

#endif // MY_STRING_H

==================================

mystring.cpp

#include \"mystring.h\"
#include <iostream>

#include <string.h>   

using namespace std;


myString::myString() : length(0), buff(NULL)
{
cout << \" myString default constr.\ \";
}

void myString::Put()
{

   for( int i=0; i < length; i++ ) cout<<( buff[i] );
}

myString::myString( char* cString ) :
length( my_strlen(cString) ), // count the length of init value
buff( new char[length] ) // allocate storage
{
cout << \" myString( char * ) constr.\ \";
my_strncpy( buff, cString, length ); // copy init value into storage
}


myString::myString( const myString& s ) :
length( s.length ),
buff( new char[length] )
{
  
my_strncpy( buff, s.buff, length );
}

myString::~myString()
{
cout << \" myString destr.\ \";
delete [] buff;
}

int myString::size() { return length; }
  

myString& myString::operator= ( const myString& s )
{
cout << \" myString::operator=\ \";
if( this != &s ){ // guard against a = a;
   delete [] buff; // release old memory & then
   length = s.length; // allocate new memory
   buff = new char[length];
   my_strncpy( buff, s.buff, length );
}
return *this; // return a reference to itself
}


char& myString::operator[] (int index )
{
if( index < 0 || index > length ) {
   cerr << \"Invalid index in myString\ \";
     
}
return buff[index];
}


myString myString::operator+( const myString& s1)
{
myString res;
res.length = s1.length + this->length;
res.buff = new char[ res.length ];
my_strncpy( res.buff, this->buff, this->length );
my_strncpy( res.buff + this->length, // pointer arithmetic!
   s1.buff,
   s1.length );
return res;
}

void myString::Reverse()
{

   char result[20];

   for (int i = 0; i<length; ++i)
   {
       result[i]=buff[length-i-1];
   }
  

   my_strncpy(buff,result,length);
}

ostream& operator<< ( ostream& os, const myString& s )
{
// print char after char from buff
for( int i=0; i < s.length; i++ ) os.put( s.buff[i] );
return os; // this is to allow multiple <<, as in cout << a << b;
}   

=======================================

stringtest.cpp

#include <iostream>
#include \"mystring.cpp\"
using namespace std;

int main(){
  
myString a(\"Akshay\");

myString b(\"Abhang\");

myString c=a+b;
c.Put();
c.Reverse();
cout<<\"\ After reverse \ \";
c.Put();

return 0;
}

A string in C++ is simply an array of characters with the null character(\\0) used to mark the end of the string. C++ provides a set of string handling function
A string in C++ is simply an array of characters with the null character(\\0) used to mark the end of the string. C++ provides a set of string handling function
A string in C++ is simply an array of characters with the null character(\\0) used to mark the end of the string. C++ provides a set of string handling function
A string in C++ is simply an array of characters with the null character(\\0) used to mark the end of the string. C++ provides a set of string handling function
A string in C++ is simply an array of characters with the null character(\\0) used to mark the end of the string. C++ provides a set of string handling function

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site