string at() function in C++


In this section, we will see what is the at a () function in C++. The at() function is used to access the character at a given position.

In this program, we will iterate through each character using at a () function and print them into different lines.

Example Code

 Live Demo

#include<iostream>
using namespace std;

main() {
   string my_str = "Hello World";

   for(int i = 0; i<my_str.length(); i++) {
      cout << my_str.at(i) << endl; //get character at position i
   }
}

Output

H
e
l
l
o

W
o
r
l
d

Updated on: 30-Jul-2019

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements