- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
String at () in C++
Abstract
This short tutorial is an overview of the C++ String class at() functionality in order to access a sequence of characters from the string. In the forthcoming section, an aspiring reader can through string class programming examples to get a thorough understanding of the manipulation of at() functions.
String Class
In programming terms, the strings are typical, a double-quotes representation, contain a collection of characters. The string class is a container class too, to iterate over all its characters using an iterator operator []. Besides, The String class handles all operations related to memory and null termination. Anyone can perform plenty of string related operations including comparison, copying, concatenation, searching, sorting and many more by harnessing its essentials function, in which one of the important methods at () used to extract characters from a string specific position.
String.at () Method
The at() method of String class is designed to access a particular character residing in a string. It automatically assesses whether pos is the valid position of a character in the given string, otherwise, it throws an “out_of_range” exception. The usage of syntax is as follows;
Syntax
char& at (size_type id); const char& at (size_t pos) const;
The following C++ program construct shows the usage of the String class at() method where the program firstly, determine the length of the input string, then the at() method retrieve the particular characters from the given a given position in the string as;
Example
#include <iostream> using namespace std; void retrieveChar(string str){ char chr; // Calculating the length of string int len = str.length(); // retrieving characters for (int i = 0; i < len; i++) { chr = str.at(i); cout << chr << " "; } } int main(){ retrieveChar("ajaykumar"); return 0; }
As seen in the above code, all string operation code is bundled into the retrieveChar() method, later on, which call is passed to program main() execution. After a successful compilation of the example.CPP file, it produces the following output in which characters from ann input string is being sequentially accessed by virtue of the String class method at() as;
a j a y k u m a r
Security Note
Strings input from various sources are special concerns for the security specialist as it blatantly provides a means too, to alter the default behavior and output of the program resorting to external malicious output and special black hat hacking tact. Hackers often exploit the weaknesses in string representation vulnerability and cause fatal damage to the program. So, it is highly suggested to ensure appropriate string related measures and guidelines in the program to futile any threats.
Conclusion
So, we have deep dig into the string class and its core method at() along with its usage syntax in an account of accessing characters from a string in a sequential manner in the aforesaid code snippet. Finally, we get a surfaced touch of string related vulnerability and weak representation which often leads to exploitation like buffer overflow, cross-site scripting, Format string, canonicalization, and many more notable bugs.