Loading....
Coupon Accepted Successfully!

 

Miscellaneous Functions

assign()

This function is used to assign a string wholly/ partly to another string object. It is used in the following format:

s2.assign (s1)

where s1 and s2 are two string objects. The contents of the string s1 are assigned to s2.

S2.assign (s1,0,5);

In the above format, elements from 0 to 5 are assigned to the object s2.

18.24 Write a program to assign a substring from main string to another string object.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1(“c plus plus”);

string s2;

int x=0;

s2.assign(s1,0,6);

cout<<s2;

return 0;

}

OUTPUT

C plus

Explanation: In the above program, s1 and s2 are two string objects. The object s1 is initialized with the string “c plus plus”. The object s2 invokes the assign() function with the integer argument 0,6, which indicates the sub-string. The sub-string is assigned to the object s2, that is, “C plus”.

begin()

This member function returns the reference of the first character of the string. It is used in the following format:

x = s1.begin();

where x is a character pointer, and s1 is a string object.

18.25 Write a program to find starting character of a given string.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1(“C plus plus”);

char *x;

x=s1.begin();

cout<<*x;

return 0;

}

OUTPUT

C

Explanation: In the above program, s1 is a string object that is initialized with string “C plus plus”. The x is a character pointer. The object s1 invokes the function begin() and returns the starting address of the string s1 to the character pointer x. The contents of x printed are ‘C’; that is, the first character of the string.





Test Your Skills Now!
Take a Quiz now
Reviewer Name