Operation on string - c ++ program, C/C++ Programming

Operation on String - C ++ Program:

Write a program to define operations on string in c++.

class String {

   char *char_ptr;   // pointer to string contents

   int length;       // length of string in characters

public:

   // three different constructors

   String(char *text);           // constructor using existing string

   String(int size = 80);        // creates default empty string

   String(String& Other_String); // for assignment from another

                                 // object of this class

   ~String() {delete char_ptr;};

   int Get_len (void);

   void Show (void);

};

 

String::String (char *text)

{

   length = strlen(text);  // get length of text

   char_ptr = new char[length + 1];

   strcpy(char_ptr, text);

};

 

String::String (int size)

{

   length = size;

   char_ptr = new char[length+1];

   *char_ptr = '\0';

};

 

String::String (String& Other_String)

{

   length = Other_String.length;       // length of other string

   char_ptr = new char [length + 1];   // allocate the memory

   strcpy (char_ptr, Other_String.char_ptr); // copy the text

};

 

int String::Get_len(void)

{

   return (length);

};

 

void String::Show(void)

{

   cout << char_ptr << "\n";

};

 

main ()                                // test the functions

{

   String AString ("Allocated from a constant string.");

   AString.Show();

 

   String BString;             // uses default length

   cout << "\n" << BString.Get_len() << "\n" ; //display length

   BString = "This is BString";

 

   String CString(BString);    // invokes the third constructor

   CString.Show();             // note its contents

}

Posted Date: 10/19/2012 3:07:24 AM | Location : United States







Related Discussions:- Operation on string - c ++ program, Assignment Help, Ask Question on Operation on string - c ++ program, Get Answer, Expert's Help, Operation on string - c ++ program Discussions

Write discussion on Operation on string - c ++ program
Your posts are moderated
Related Questions
A: this is a procedure during exception handling while the destructor is called for all local objects in the stack among the place where the exception was thrown & where this is ca

.Develop a two dimensional interactive game with the following features: 1. Use OpenGL (any version you find convenient) 2. The game can either be two player or one player with the

A cl a s s c a n b e d e f i n e d i n s i d e a b l o c k o r f u n c t i o n.   T h i s t yp e cla s s i s known

Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible?

A: They present a degree of freedom in the interface design options. Member functions & friend functions are equally privileged (100% vested). The major difference is that a fri

how to convert a general tree into binary tree with example


Define Commonly Used Built-in Library Functions? Comprise opened a file pointer you will desire to use it for either input or output. The C language supplies a set of functions

write a program to find the area under the curve y=f(x) between x=a and x=b integrate y=f(x)   #include float start_point, /* GLOBAL VARIABLES */

world wide web