There are mainly two types of handling strings such as using null terminated character arrays and using object of type string. The main drawback of null terminated strings is that most of the C++ operators cannot be applied on them. In order to overcome this drawback standard string class is used. If a string class is used normal operations can be performed on them like any other data type. When null terminated character arrays are declared memory space is wasted because you define the length of the array more than actually it is used. The use of string class does not waste memory space. In order to use string class header file <string> is included in the program. The general form of how string object is created is:-
The object_name is the name of the object. For example,
name is the object of type string. The operators which can be applied on object of string type are:-
+= Concatenation Assignment
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal
A string object can be assigned to another string object or another string. For example,
objects name and name 1 will contain same string.
The two strings can be concatenated using ‘+’ operator. For example,
fullname= firstname + “ “ + lastname;
The string objects firstname, whitespace and lastname are concatenated and assigned to string object fullname.
The equality operator ‘==’ checks whether the two strings are equal or not.
The operator will return true because both string objects have same value.
The inequality operator ‘!=’ returns true if two string objects are not equal. For example,
The operator will return true because name1 is not equal to name 2.
The less than operator checks whether the string objects come before another string object. It returns true if one string object in alphabetical order comes before another string object. For example,
cout << “cat comes before dog” << endl;
The message “cat comes before dog” will be displayed as in alphabetical order word cat will appear before word dog.
The greater than operator ‘>’ checks whether one string object comes after another string object. It returns true if one string object in alphabetical order comes after another string object. For example,
cout << “dog comes after cat” << endl;
The message “dog comes after cat” will be displayed as alphabetically word dog comes after word cat.
The subscript operator ‘’ is used to access a character of the string just like null terminated character arrays. The subscript returns a char value. For example,
The character ch will contain letter ‘c’. The name1 will become will change to “dat”.
Input and Output
The insertion operator ‘<<’ writes the value of object to the output stream. The extraction operator ‘>>’ reads the character string from the input stream and assigns it to the string object. For example,
cin >> name;
cout << name << endl;
The user will enter the name and the same name will be displayed on the screen.
Here is a program which shows the working of string class.
using namespace std;
cout << "Enter your first name" << endl;
cin >> name1;
cout << "Enter your last name" << endl;
cin >> name2;
name3=name1 + " " + name2;
cout << "First name alphabetically comes before last name" << endl;
cout << "Last name alphabetically comes before first name" << endl;
cout << "First name and last name are same" << endl;
cout << "Your full name is : " << name3 << endl;
cout << "The initals of the name are " << a << "." << b << "." << endl;
The result of the program is:-
includes a header file string into the program. The statement
name3=name1 + " " + name2;
concatenate two string objects and whitespace. The statement
checks whether name1 alphabetically comes before name2. The statement
checks whether name1 alphabetically comes after name2. The statement
checks whether two string objects are equal or not. The statements
assign character a and b the first letter of name1 and name2 respectively.
Go to the previous lesson or proceed to the next lesson