Loading....
Coupon Accepted Successfully!

 

Accessing Elements Of Strings

It is possible to access a particular word or a single character of a string with the help of a member function of the string class. The supporting functions are illustrated with suitable examples.

at()

This function is used to access an individual character. It requires one argument that indicates the element number. It is used in the following format:

s1.at (5);

where s1 is a string object, and 5 indicates the 5th element that is to be accessed.

18.17 Write a program to display the string elements one by one. Use the member function at().

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1(“PROGRAMMING”);

for (int j=0;j<s1.length();j++)

cout<<s1.at(j);

return 0;

}

OUTPUT

PROGRAMMING

Explanation: In the above program, s1 is a string object that is initialized with the string “PROGRAMMING.” The for loop is used to represent a successive character location. The at() function with a one-integer argument displays the characters. The variable j indicates the element number.

The statement cout<<s1.at (j); displays the characters. We can also use the overloaded operator[] to display the string without use of the at() function. Thus, the statement would be cout<<s1. [j].

find()

The find() member function finds the given sub-string in the main string. It is used in the following format:

s1.find (s2);

where s1 and s2 are string objects. The find() function searches for the sub-string s2 in the main string s1.

18.18 Write a program to find sub-string from the source string.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1 (“Bangalore is the capital of Karnataka”);

int x=s1.find(“capital”);

cout<<“Capital is found at:”<<x;

return 0;

}

OUTPUT

Capital is found at : 17

Explanation: In the above program, s1 is a string object that is initialized with the string. The find() member function finds the given string in the source string. In this program, the find() function searches for the word “capital” in the string s1. The find() function returns the element number of the previous element from where the sub-string starts.

substr()

The member function substr() is used to find the sub-string in the main string. It requires two-integer arguments. The first argument indicates the starting element of the string, and the second argument indicates the last argument of the string. It is used in the following format:

s1.substr (s,e);

where s1 is a string object. The variables s and e are integer variables that indicate the starting and ending element numbers, respectively, of the sub-string.

18.19 Write a program to retrieve the substring from the main string.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1 (“C plus plus”);

cout<<s1.substr(2,4);

cout<<“\n”;

return 0;

}

OUTPUT

Plus

Explanation: In the above program, s1 is declared as a string object. It is initialized with the string “C plus plus.” The member function substr() requires two-integer arguments, which indicate the starting and ending elements of the sub-string. The sub-string is displayed on the screen.

find_first_of()

This member function is used to find the first occurrence of the given character(s). It is used in the following format:

s1.find_first_of (‘p’);

where s1 is a string object, and p is a character whose first occurrence is to be found.

18.20 Write a program to find the first occurrence of the given character. Use member function find_first_of().

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1 (“C plus plus”);

cout<<s1.find_first_of(‘p’);

cout<<“\n”;

return 0;

}

OUTPUT

2

Explanation: In the above program, s1 is declared as a string object that is initialized with the string “C plus plus.” The member function find_first_of() searches for the character ‘p’ and when it finds it, it returns the element number of the previous character.

find_last_of()

This member function is used to find the last occurrence of the given character(s). It is used in the following format:

s1.find_last_of (‘p’);

where s1 is a string object, and p is a character whose last occurrence is to be found.

18.21 Write a program to find the last occurrence of the given character. Use member function find_last_of().

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1 (“C plus plus”);

cout<<s1.find_last_of(‘p’);

cout<<“\n”;

return 0;

}

OUTPUT

7

Explanation: In the above program, s1 is declared as a string object that is initialized with the string “C plus plus.” The member function find_last_of() searches for the character p and when its last occurrence is found, it returns the element number of the previous character.





Test Your Skills Now!
Take a Quiz now
Reviewer Name