string at() in C++

at(): It provides access to the data contained in the string at the given position. 

It returns a read/write reference to the character. So by this, we can change the value at that position or print the value at that position.

Parameters:

__n:– The index of the character to access.

Syntax:

str.at(__n)

For Example:

str  ="abc"

str.at(1) => It returns the reference to 1 position of the string (i.e b).

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str = "abc";
    cout << str.at(1<< "\n";
  
    return 0;
}


No comments:

Post a Comment