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
1. Jewels can only be removed for polishing from either end of the necklace (i.e. head or tail) 2. Once a jewel type is removed from the necklace, all other instances of the same j

I need website Product section search box coding section fix Project Description: On our products section in the search box it only searches the name and title of our product

Write a program to create a binary file and store the following data "hello", 0x0030,'1', 1.234  Using visual studio examine the binary file and note how the day is stored   Answe

create two array names and grade .in first array you have to store thease 10 names 1.a,2.3.r,4.s,5.l,6.k,7.n,8.b,9.z,10.d you have to enter grades for thease students.te program mu

HWID Spoofer for Windows 7, 8 x64 Project Description: I want a simple program that will spoof my hardware id. I should be able to prepare the id to spoof to in the program a

i need a program that generates this output: The highest card wins! Suit Order is: clubs, diamonds, hearts and spades The computers card is the 5 of Spades. The player’s card is t

how much is it to fix a small data struct in a sorted list that pass itemtypes. all the code is written just logical errors

(c) A static member function can access static member data only, static member functions and functions and data outside the class. A static member function may be called, even whil


what is the first thing I need to know about Programming?