Loading....
Coupon Accepted Successfully!

 

String Attributes

The various attributes of a string such as size, length, and capacity can be obtained using member functions. The size of the string indicates the total number of elements currently stored in the string. The capacity means the total number of elements that can be stored in a string. The maximum size means the largest valid size of the string supported by the system.

size()

The member function size() determines the size of the string object, that is, the number of bytes occupied by the string object. It is used in the following format:

s1.size()

where s1 is a string object, and size() is a member function.

18.12 Write a program to display the size of the string object before and after initia­lization.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1;

cout<<“\nsize:”<<s1.size();

s1=“hello”;

cout<<“\nNow size:”<<s1.size();

return 0;

}

OUTPUT

size : 0

Now size : 5

Explanation: In the above program, s1 is a string object. In the first cout statement, the s1 invokes the member function size(), which returns the size of the string object s1. The size() object returns the size zero, because the object s1 is empty. The object s1 is initialized with the string “hello”. In the second cout statement again, the object s1 invokes the member function size(). This time, the size() member function returns the size 5. Thus, the size() function determines the size of the string object.

length()

The member function length() determines the length of the string object, that is, the number of characters present in the string. It is used in the following format:

s1.length()

where s1 is a string object, and length() is a member function.

The member function size() and length()exhibit a similar result. Each character occupies one byte in the memory. The number of bytes and the total number of elements present in the string are always same; hence, both these functions exhibit a similar result.

18.13 Write a program to calculate the length of the string. Use member function length().

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1;

cout<<“\n Length:”<<s1.length();

s1=“hello”;

cout<<“\n Now Length:”<<s1.length();

return 0;

}

OUTPUT

Length : 0

Now Length : 5

Explanation: The above program is similar to the previous one. Here, instead of the size() function, the length() member function is used. The length() member function displays the length of the string object before and after initialization.

capacity()

The member function capacity() determines the capacity of the string object, that is, the number of characters it can hold. It is used in the following format:

s1.capacity()

where s1 is a string object, and capacity() is a member function.

18.14 Write a program to display the capacity of the string object. Use member function capacity().

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1;

cout<<“\n Capacity:”<<s1.capacity();

s1=“hello”;

cout<<“\n Capacity:”<<s1.capacity();

s1=“abcdefghijklmnopqrstuvwxyzabcdef”;

cout<<“\n Capacity:”<<s1.capacity();

return 0;

}

OUTPUT

Capacity : 0

Capacity : 31

Capacity : 63

Explanation: In the above program, s1 is declared as a string object. The member function capacity() returns the capacity of the string object to hold the character elements. The maximum size of the string in this system is 4294967293 that is obtained by the member functionmax_size(). In this program, the capacity() function returns 31. We get different values in various situations. If the string object is empty, the function returns 0. In case the string object contains a string less than 32 characters, it returns 31. In this program, the string is initialized with 32 characters. The capacity() function returns the value 63, that is, 32 + 31.

max_size()

The member function max_size() determines the maximum size of the string object, that is, the number of characters it can hold. It is used in the following format:

s1.max_size()

where s1 is a string object, and max_size() is a member function.

18.15 Write a program to display the maximum size of the string object.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1;

cout<<“\n Maximum size:”<<s1.max_size();

return 0;

}

OUTPUT

Maximum size : 4294967293

Explanation: In the above program, the s1 is declared as a string object. The max_size() function returns the maximum size of the string object, that is, 4294967293.

empty()

The empty() function determines whether the string is empty or filled. If the string is empty, it returns 1; otherwise, it returns 0. It is used in the following format:

s1.empty()

where s1 is a string object, and empty() is a member function.

18.16 Write a program to determine whether the string object is initialized or not. Use empty() member function.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1;

cout<<“\n Empty:”<<(s1.empty()? “True”:“False”);

s1=“abc”;

cout<<“\n Empty:”<<(s1.empty() ? “True”:“False”);

return 0;

}

OUTPUT

Empty : True

Empty : False

Explanation: In the above program, s1 is declared as a string object, and it is not initialized. The member function empty() is called with a conditional operator. It displays the message true (1), that is, the string is empty.

The string object s1 is initialized with the string “abc” and again, the empty() function is invoked. This time, it returns false (0), that is, the string is not empty.





Test Your Skills Now!
Take a Quiz now
Reviewer Name