string at() function in C++

C++Server Side ProgrammingProgramming

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
raja
Published on 01-Apr-2019 18:00:56
Advertisements