Saturday 2 February 2013

String


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:-

            string object_name;

The object_name is the name of the object. For example,

            string name;

name is the object of type string. The operators which can be applied on object of string type are:-

            Operator                                              Meaning

                =                                                      Assignment
                +                                                      Concatenation
                +=                                                    Concatenation Assignment
                ==                                                    Equality
                !=                                                     Inequality
                <                                                      Less than
>                                                      Greater than
<=                                                    Less than or equal
>=                                                    Greater than or equal
[]                                                      Subscripting
<<                                                    Output
>>                                                    Input

Assignment

A string object can be assigned to another string object or another string. For example,

            string    name;
            string name1=”Peeyush”;
            name=name1;
                                   
objects name and name 1 will contain same string.

Concatenation

The two strings can be concatenated using ‘+’ operator. For example,

            string firstname=”Peeyush”;
            string lastname=”Taori”
            string fullname;
            fullname= firstname + “  “ + lastname;

The string objects firstname, whitespace and lastname are concatenated and assigned to string object fullname.

Equality

The equality operator ‘==’ checks whether the two strings are equal or not.

            string name1=”Peter”;
            string name2=”Peter”;
            if(name1==name2)
            {         
            }
The operator will return true because both string objects have same value.

Inequality

The inequality operator ‘!=’ returns true if two string objects are not equal. For example,

            string name1=”peter”;
            string name2=”Tom”;
            if(name1!=name2)
            {
            }
The operator will return true because name1 is not equal to name 2.

Less than

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,

            string name1=”cat”;
            string name2=”dog”;
            if(name1<name2)
            {         
                        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.

Greater than

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,

            string name1=”cat”;
            string name2=”dog”;
            if(name2>name1)
            {         
                        cout << “dog comes after cat” << endl;
            }

The message “dog comes after cat” will be displayed as alphabetically word dog comes after word cat.

Subscripting

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,

            string name1=”cat”;
            char ch=name1[1];
            name1[0]=’d’;

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,
                       
string name;
                        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.

#include<iostream>
#include<string>
using namespace std;

int main()
{
            char a,b;
            string name1,name2,name3;
            cout << "Enter your first name" << endl;
            cin >> name1;
            cout << "Enter your last name" << endl;
            cin >> name2;
            name3=name1 + "  " + name2;
            if(name1<name2)
            {         
                        cout << "First name alphabetically comes before last name" << endl;
            }
            if(name1>name2)
            {
                        cout << "Last name alphabetically comes before first name" << endl;
            }
            if(name1==name2)
            {
                        cout << "First name and last name are same" <<  endl;
            }
            cout << "Your full name is : " << name3 << endl;
            a=name1[0];
            b=name2[0];
            cout << "The initals of the name are " << a << "." << b << "." << endl;
            return(0);
}

The result of the program is:-

program output

The statement
           
            #include<string>

includes a header file string into the program. The statement
                       
            name3=name1 + "  " + name2;

concatenate two string objects and whitespace. The statement

                        if(name1<name2)

checks whether name1 alphabetically comes before name2. The statement

                        if(name1>name2)

checks whether name1 alphabetically comes after name2. The statement

                        if(name1==name2)

checks whether two string objects are equal or not. The statements

                        a=name1[0];
                        b=name2[0];

assign character a and b the first letter of name1 and name2 respectively.

No comments:

Post a Comment