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 print any name on screen 10 times, WAP TO PRINT ANY NAME ON SCREEN 1...

WAP TO PRINT ANY NAME ON SCREEN 10 TIMES void main () { int a=1; clrscr(); do { printf ("expertsmind\n"); a++; } while (a getch (); }

Copy constructor and an overloaded assignment operator, What is the differe...

What is the difference among a copy constructor and an overloaded assignment operator? Ans) A copy constructor constructs a latest object by using the content of the argument

Explain friend for overloading operators, Friend for Overloading Operators ...

Friend for Overloading Operators Sometimes friend functions cannot be avoided. For example with the operator overloading. Consider the following class that have data members to

Program of 2d byte array dynamicall, In this lab, please complete a given p...

In this lab, please complete a given program to perform the following tasks: 1. Allocate a 10 by 5 2D byte array dynamically. The way of allocation must be consistent with page

Scrape a site and save as csv, Scrape a site and save as csv Project Des...

Scrape a site and save as csv Project Description: I want a programmer to create scraping software. I'll want every page scraped of name, job title, company, and url. There a

ALGORITHM, write a pseudocode algorithm for a program that accepts a number...

write a pseudocode algorithm for a program that accepts a number and prints out its reciprocal(1/n) is required.the program should prevent the user from entering zero by asking t

Super ascii, program to convert string from super ascii string with minimum...

program to convert string from super ascii string with minimum cost

C++ code, write c++ programm calculate electricity bill with person name,us...

write c++ programm calculate electricity bill with person name,use ,id

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