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

Assignment Help:

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

}


Related Discussions:- Operation on string - c ++ program

Wap to swap the three digit number, WAP TO SWAP THE THREE DIGIT NUMBER ...

WAP TO SWAP THE THREE DIGIT NUMBER void main () { int b,r,n,r1,r2; clrscr (); printf ("Enter the Value: "); scanf ("%d",&n); r=n%10; n=n/10; r1=n%10;

Find out the largest torque, Find out the largest torque: A flat belt...

Find out the largest torque: A flat belt is taken in use to transmit torque from pulley A to pulley B as shown in figure given below. The radius of each pulley is 50mm and co

Super ansii prgm, Ask quIn the Byteland country a string "s" is said to sup...

Ask quIn the Byteland country a string "s" is said to super ascii string if and only if count of each charecter in the string is equal to its ascci value in the byteland country as

C , why c is middle level language?

why c is middle level language?

Compiler design-limit the methods, Problem : Compiler Design - Limit the me...

Problem : Compiler Design - Limit the methods Rahul is a newbie to the programming and while learning the programming language he came to know the following rules: ·

The car’s measurements are illustrated, The car’s measurements are illustra...

The car’s measurements are illustrated, using two arrays. Array 1 = {L, R, L, R, R, L, R, R, L, R, R, L, R, L, L, R, Z}

Decodethecode, 6999066263304447777077766622337778 -----> message sent by th...

6999066263304447777077766622337778 -----> message sent by the first smuggler. my name is robert---------> message decoded by the second smuggler. Where ‘0’ denotes the "space".

Vision based simultaneous mapping and localization, Project Description: ...

Project Description: Design and prepare software that can navigate a mobile robot using SLAM technique using vision sensor (camera). Skills required are C Programming, Engine

What is threaded binary tree, Threaded binary tree: Consider the linked il...

Threaded binary tree: Consider the linked illustration of a binary tree 'T.  Approximately half of the entries is the pointer fields 'LEFT' and 'RIGHT' will have null elements. Th

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd