Loading....
Coupon Accepted Successfully!

 

Declaring And Initializing String Objects

In C, we declared a string as follows:

char text[10];

where, as in C++, a string is declared as an object. The string object declaration and initialization can be done once by using a constructor of the string class. The constructors of the string class are described in Table 18.2.

127226.jpgTable 18.2 String constructors

Constructors

Meaning

string();

Produces an empty string

string (const char *text);

Produces a string object from a null-ended string

string (const string & text);

Produces a string object from another string object

We can declare and initialize string objects as follows:

Declaration of String Objects

string text; // Using constructors without
//
arguments

string text(“C++”); // Using constructors with one
//
argument

text1=text2 // Assignment of two string objects

text =“c++”+ text1 // Concatenation of string objects

cin>> text // Reading string without spaces
//
through the keyboard

getline (cin, text) // Reading string with blank spaces

Two string objects can be concatenated using overloaded + operator. The overloaded += operator appends one string to the end of another string. The operators << and >> are overloaded operators and can be used for input and output operations.

text1+=text is equivalent to text1=text1+text

text1+=“xyz” is equivalent to text1=text1+“xyz”

cin >> text // Reads string without spaces

cout<<text // Displays the contents on the screen

getline (cin,text) // Reads string with blank spaces

Here are some illustrations based on the above concepts:

18.4 Write a program to declare string objects. Perform assignment and concatenation operations with the string objects.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string text; // Vacant string object

string text1(“C++”); // Constructor with one argument

string text2(“OOP”);

cout<<“text1:”<<text1 <<“\n”;

cout<<“text2:”<<text2 <<“\n”;

text=text1; // assignment operation

cout<<“text:”<<text<<“\n”;

text=text1+“ + text2;

cout<<“Now text:”<<text;

return 0;

}

OUTPUT

text1 : C++

text2 : OOP

text : C++

Now text : C++ OOP

Explanation: In the above program, three-string objects text, text1, and text2 are defined. The objects are not initialized. The objects text1 and text2 are initialized with the strings “C++” and “OOP” respectively. The object text is initialized with the contents of objecttext1 using ‘=’ operator as per the statement text = text1. Again, the joining of objects text1 and text2 is done, and the resulting string is assigned to the object text as per the statement text = text1+” + text2.

The Table 18.3 describes various member functions of string class.

127386.jpgTable 18.3 String manipulating functions

FUNCTION

USE

append()

Adds one string at the end of another string

assign()

Assigns a specified part of a string

at()

Accesses the characters at a given location

begin()

Returns a reference to the beginning of a string

capacity()

Calculates the total elements that can be stored

compare()

Compares two strings

empty()

Returns false if the string is not empty; otherwise, it is true

end()

Returns a reference to the termination of a string

erase()

Erases the specified character

find()

Finds the given sub-string in the source string

insert()

Inserts a character at a given location

length()

Calculates the total number of elements of a string

max_size()

Calculates the maximum possible size of a string in a given system

replace()

Substitutes the specified characters with a given string

resize()

Modifies the size of the string as specified

size()

Provides the number of characters in the string

swap()

Exchanges the given string with another string

The operators used with arithmetic or comparison operations can be used with string objects. Table 18.4 describes these operators.

130850.jpgTable 18.4 String manipulating operators

Operator

Working

=

Assignment

+

Joining two or more strings

+=

Concatenation and assignment

= =

Equality

! =

Not equal to

<

Less than

<=

Less than or equal to

>

Greater than

>=

Greater than or equal to

[]

Subscription (used with array)

<<

Insertion operator

>>

Extraction operator





Test Your Skills Now!
Take a Quiz now
Reviewer Name