Loading....
Coupon Accepted Successfully!

 

Handling String Objects

The member functions insert(), replace(), erase(), and append() are used to modify the string contents. The following program illustrates the use of these functions:

insert()

This member function is used to insert a specified string into another string at a given location. It is used in the following form:

s1.insert(3,s2);

where s1 and s2 are string objects. The first argument is the location number in the calling string where the second string is to be inserted.

18.8 Write a program to insert one string into another string using insert() function.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1(“abchijk”);

string s2(“defg”);

cout<<“\n s1=”<<s1;

cout<<“\n s2=”<<s2;

cout<<“\n after insertion”;

s1.insert(3,s2);

cout<<“\n s1=”<<s1;

cout<<“\n s2=”<<s2;

return 0;

}

OUTPUT

s1= abchijk

s2= defg

after insertion

s1= abcdefghijk

s2= defg

Explanation: In the above program, two string objects s1 and s2 are declared and initialized with the strings “abchijk” and “defg”. The insert() function inserts the string “defg” in the string “abchijk” at location 3. Now, the resulting string is “abcdefghijk”. In the statement s1. insert (3,s2), the object s1 invokes the member function insert() and passes the arguments 3 and s2 explicitly.

erase()

The erase() member function is used to erase/ remove specified characters from a specified location. It is used in the following form:

s1.erase (3,7);

where s1 and s2 are string objects. The first argument is the starting element number, and the second argument is the last element number, that is, the character elements from 3 to 7 are removed.

18.9 Write a program to remove specified characters from the string.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1(“abc12345defg”);

cout<<“\n s1=”<<s1;

cout<<“\n after erase()”;

s1.erase(3,5);

cout<<“\n s1=”<<s1;

return 0;

}

OUTPUT

s1= abc12345defg

after erase()

s1= abcdefg

Explanation: In the above program, s1 is a string object that is declared and initialized with the string “abc12345defg”. The object s1 invokes the member function insert() with two integers 3 and 5. The 3 indicates the starting element number, and 5 indicates the number of characters to be erased. The insert() function erases the next five characters from the 3rd character.

replace()

This member function replaces the given characters in a string. It requires three arguments as per the following format:

s1.replace (2,5,s2);

where s1 and s2 are string objects.

18.10 Write a program to replace the string with the given string.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1(“abcdefg”);

cout<<“\n s1=”<<s1;

cout<<“\n after replace()”;

s1.replace(1,3,”BCD”);

cout<<“\n s1=”<<s1;

cout<<“\n”;

return 0;

}

OUTPUT

s1= abcdefg

after replace()

s1= aBCDefg

Explanation: In the above program, s1 is a string object initialized with “abcdefg”. The object s1 invokes the member function replace() with three arguments. The first argument indicates the starting character element, the second argument indicates the location of the last character, and the third argument is a string that is to be replaced.

append()

The above function is used to add a string at the end of another string. It is used in the following format:

s1.append (s2);

where s1 and s2 are two objects. The contents of s2 are appended in the string s1.

18.11 Write a program to append one string at the end of another string. Use append() function.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1(“abcdefg”);

string s2(“hijklmn”);

cout<<“\n s1=”<<s1;

cout<<“\n after append()”;

s1.append(s2);

cout<<“\n s1=”<<s1;

cout<<“\n”;

return 0;

}

OUTPUT

s1= abcdefg

after append()

s1= abcdefghijklmn

Explanation: In the above program, two-string objects s1 and s2 are declared and initialized with the strings “abcdefg” and “hijklmn”. The s1 object invokes the member function append(), and s2 is passed as an argument. The string s2 is added at the end of the string s1.





Test Your Skills Now!
Take a Quiz now
Reviewer Name