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
Define hash functions. Explain the Division method, Mid square method and Folding method of hash functions.

Write a Program that illustrate union declaration? A C program encloses the following union declaration: union id { char color[12]; int size; } shirt, blouse; At this

how to make a program this struct items/product

the program that solve the efficiency of otto cycle


Define some Important Point for bit fields with structures? Only n lower bits will be assigned to an n bit number thus type cannot take values larger than 15 (4 bits long)

Define the Multi Dimensional Arrays? Consequently far we have considered arrays with only one dimension. It is as well possible to define an array which is having a 2-dimension

C program to check display days and month main() {           int months, days;           clrscr();           printf("ENTER DAYS: ");           scanf("%d", &days);

Introduction to I/O interfacing, discuss microcontrollers applications in industry. Detailed product design specifications that have sections on both hardware and software inclu

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send