String Attributes
· The various attributes of a string such as
· SIZE , length , and capacity, empty , find,substring,compare can be obtained using member
functions.
SIZE
· The size of the string indicates the total number of elements currently stored in the string.
capacity
· The capacity means the total numbers of elements that can be stored in a string.
· the maximums size means the largest valid size of the string supported by the system.
length
· The member function length() determines the length of the string object , that is , the number of
characters present in the string.
empty
· The empty function determines whether the strin is empty or filled. If the string is empty , it
returns 1 , otherwise it returns 0 .
find
· The find member funtion finds the given sub-string in the main string.
compare
The member function compare is used to compare two strings or sub-string. It returns 0 when the two
strings are similer , otherwise , it returns a non-zero value
Write a program for string attributes
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
string s1;
s1="Hello";
s2="hello";
cout<<"\n Size of s1 is:"<<s1.size();
cout<<"\n capacity s1 is:"<<s1.capacity();
cout<<"\n S1 is empty:"<<s1.empty();
cout<<"\n find llo in hello:"<<s1.find("llo");
cout<<"\n compare string is:"<<s1.compare(s2);
getch();
OUTPUT
Size of s1 is: 6
capacity s1 is: 31
S1 is empty : 0
find llo in hello : 1
compare string is: false