C++ manipulation string using operators

C++ Manipulation of string using operators

ANSI C implements string character arrays, pointers and string functions. There are no operators for manipulation the strings. once of the main drawbacks of string manipulations in C is that whenever a string is to be copied, The programmer must fist determine its length and allocate the required amount of memory

Although these limitations exist in C++ as well, its permits us to create own definitions of operators that can be used to manipulation the any string very much similar to the decimal number(Recently ANSI C++ committee has added a new class called string to the C++) class library that supports all types of string manipulations. String manipulations using the string class are discuss in the next pages

For example, we shall be able to use statements like

string3=string1+string2;
if(string1>=string2) string =string1;

string can be defined as class objects which can be then manipulated like the built in types. since the strings vary greatly in size, we use new operator to allocate memory for each string and a pointer variable to point to the string array. Thus we must create string objects that can hold these two pieces of information, namely,locations and length which are necessary for strinf manipulations. A typical string class will look as follows

class string
{
char *p; // pointer to string
int len; // length of string
public:
……. // member functions
…… // to initialize and
……. // manipulate strings
};

we shall consider an example to illustrate the application of overloaded operators to strings The example shown in program below overloads two operators, + and <= just to show how they are implemented. This can be extended to cover other operators as well

MATHMATICAL OPERATIONS ON STRINGS
#include <string.h>
#include <iostream>
class String
{
char *p;
int len;
public :
String() // create null string
{
len=0;
p=0;
};
String(const char *s); // create string from arrays
String(const String & s); // copy constructor
~String() // destructor
{
delete p;
}
//+ operator
friend String operator+(const String &s,const String &t); // <= operator
friend int operator<=(const String &s, const String &t);
friend void show(const String s);
};
String :: String(const char *s)
{
p=new char[len+1];
strcpy(p,s);
}
String:: String(const String &s)
{
len=s.len;
p=new char[len+1];
strcpy(p,s.p);
}

// overloading + operator
String operator +(const String &s, const String &t)
{
String temp;
temp.len=s.len+t.len;
temp.p=new char[temp.len+1];
strcpy(temp.p,s.p);
strcat(temp.p,t.p);
return(temp);
}
// overloading <= operator
int operator<=(const String &s, const String &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if (m<=n)
return (1);
else
return (0);
}
void show(const String s)
{
std::cout<<s.p;
}

int main()
{
String s1="new";
String s2="york";
String s3="delhi";
String t1,t2,t3;
t1=s1;
t2=s2;
t3=s1+s3;
std::cout << "\n t1=";
show(t1);
std::cout << "\n t2=";
show(t2);
std::cout << "\n";
std::cout << "\nt3=";
show(t3);
std::cout << "\n\n";
if (t1<=t3)
{
show(t1);
std::cout << "Smaller than";
show(t2);
std::cout << "\n";
}
else
{
show(t3);
std::cout << "smaller than ";
show(t1);
std::cout << "\n";
}
return 0;
} ;

The output
t1=New
t2=york
t3=new delhi
new smaller than new delhi

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise