String operations operator overloading
Different String operations:
i. = Equality
ii. == String Copy
iii. + Concatenation
iv. << To display a string
v. >> To reverse a string
vi. Function to determine whether a string is a palindrome
To find occurrence of a sub-strings. Use Operator Overloading.
Strings in C++:
Strings can be defined as class objects which can be then manipulated like a built-in data types. Since the strings vary greatly in size, we use "new" 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 length, and location which are necessary for string manipulations. A typical string class will look as
class string
{
char *p;
int len; // length of string
public:………….
// mem fun to initialize and manipulate strings
…………..
};
Pointers are used to allocate arrays dynamically i.e. we can decide the array size at runtime.
c++ incorporates the option to use standard operators to perform operations with classes in addition to with fundamental types. For example:
int a, b, c;
a = b + c;
This is obviously valid code in C++ since the different variables of the addition are all fundamental types. Nevertheless, it is not so obvious that we could perform an operation similar to the following one:
struct
{
string product;
float price;
}
a, b, c;
a = b + c;
In fact, this will cause a compilation error, since we have not defined the behavior our class should have with addition operations. However, thanks to the C++ feature to overload operators, we can design classes able to perform operations using standard operators.
Thanks
Mukesh Rajput
Post A Comment:
0 comments: