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


   // 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.");



   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
What's the order that objects in an array are destructed? Need assistance please explain it with example.

Define Static Storage Class in C Programming? The Static declarations have distinct and important uses. This allows the local variable to keep its previous value when the block

get coding for padovan string

WAP TO PRINT THE LARGEST NUMBER FROM ANY 10 NUMBERS #include stdio.h> #include conio.h>   void main()   {                    int a[10],i,max;

write the program to accept date and to say valid or invalid

Implement (a part of) electronic election Scenario where following political parties are participating in election: PPP, PML and PTI.  Create a class PollingStation where it stor

Part 1 Assignment:  develop a calculator in MASM. Text chapters covered:  1 through 4, 5.4, 5.5, 6.3, 7.4 You will develop a "calculator" algorithm in MASM using reverse-

Call by Reference Passing variables(parameters) to a function in C can be done in two ways - pass by value, also called as call by value and pass by address or also known as ca

What if one forget the [] when deleteing array allocated through new T[n]? A: All life comes to a disastrous end. It is the programmer's responsibility not the compiler's to

in this problem you are given a board in which some of the elements are placed..each element represent ancolor.fill the other elements in the board such that none of the adjacent e