accessing a character of object of string class – CSKC
Sign In

Search

Code Learning #CPP#3045    siteicon   siteicon  

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

TC++ #3045

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

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



Share

Created: 26-Jan-2018 19:18 | Updated: 26-Jan-2018 19:23|