accessing a character of object of string class
A program to show individual characters in a string class based object are accessed.
Learning Objectives
- Accessing the string object using the c-style method of array index.
- Accessing string class using the at(<index>) method.
Source Code
|
|
Run Output
Code Understanding
string m=”Yoga is a way of life”; //Declaring and initialising a string class based object
cout<<m[0]<<m[1]<<m[2]<<m[3]<<endl;
Accessing each character in exactly same way as we do for a char array in a c-style string. First character is at index 0 and then so on ..
cout<<m.at(0)<<m.at(1)<<m.at(2)<<m.at(3)<<endl;
Accessing using the at function again using the index position
Suggested Filename(s): string-char-access.cpp
Created: 26-Jan-2018 19:18 | Updated: 26-Jan-2018 19:23|
