C++

C++ string::at() Function

Today, we’re going to study one of the C++ string at() methods, and we will use a variety of examples to demonstrate how to transform string at() methods in C++ language.

As we know, it is an object-oriented programming language that gives programs a clear structure, making it possible for code to be read within the same program. C++ is a relatively basic and easy-to-understand language.

Introduction

In C++, a bundle of various characters or elements is contained in one of the C++ datatypes called a string enclosed in double quotation marks. The C++ string performs a wide range of methods, and the at() method is one of those methods. The string at() method is used to access the exact position of the character or element from the string. In simple words, in the at() method, we can access the individual character from the whole input string at the specified location. Now, let’s discuss the at() method, and let’s see how this method works.

Syntax

Here is the syntax of the string at() method, and it lets us understand how we implement it. To call the string at() method, we first write the predefined keyword, which is “char”. It will tell the compiler that we are accessing a character from the input character string. Then we will write the variable name of the input string (the variable where we have stored the input string) and concatenate it with the at() method. In the aSt() method, we will pass some arguments.

Parameter

idx: the index number of the input string from where we want to access the element of the input string. Keep in mind that the index number will be less than or equal to the length of the input string.

size_type: an unsigned integer used to display the size in bytes of any object.

Return Value

In return, we will get the exact location of the input string character, and then we can access the character by passing the index number in the at() method.

Errors & Exceptions

There is no exception if we enter the index value of the string character as less than or equal to the input string length. If we pass the index greater than the length of the input string, then the exception thrown will be out of range.

Example 01

Now, let’s start explaining our first and simple example of the string at() method. We need any C++ compiler compatible with the string methods to implement our program. To code the Program in C++, we always need basic libraries to use manipulators of C++ in the existing program. The first library we are using in this program is “#include <iostream>”. The “#” sign instructs the compiler to load the header file, the “include” keyword incorporates the header file into the program, and the “iostream” specifies inputting the data from the user and display of data.

To use strings and string methods across the entire program, we have included the second header file, which is “#include <string>”. Then we used the “using namespace std” directive, which prevents classes, functions, and variables from utilizing the same context throughout the entire program.


After importing the basic libraries and directives, we now move on to the main() function of the program. The main() function is used to write the actual line of code that we want to implement and get the results from it. In line 8, we declared a variable “str” of type “string”, and then we initialized the character string to the “str” variable. Next, we initialized another character string to the same variable “str” and printed it using the predefined cout() method of C++.

Then we want to get the size of the string we recently created. For that, we have called the size() function with the concatenation of the string variable, which is “str,” and passed the whole function into the cout() method so that we can display it. Then we also want to print the initialized capacity for the input character string. For that, we will use the capacity() function with the concatenation of the “str” variable. We have initialized the first string to get the string capacity from it.

After getting the size and capacity of the input character string, we move forward. Then we declare another variable, “res”, of type “char”, which means we are creating a character type variable. In this variable, we will store the character from the input string which we want to access. So we will call the at() method and pass the index number of the character in it and then concatenate it with the input string “str”. Then we wanted to print the element, so we used the cout() method, which is the predefined method of C++, and passed the “res” variable in it.

As discussed in the at() method, we can replace the character. For replacing each character that we have accessed is; first, we will write the variable or any symbol in a single quotation mark and then assign this to the at() method by writing the variable name “str” first and then concatenating it with the at() method and passing the index number in it. And then, we will display it by using the cout() method.

Example 02

Here is the second example of the at() method of string datatype in C++ language. The implementation of this example is the same as we have implemented above. Still, the only difference is that we are accessing only one character simultaneously. Here, we are accessing the whole input string. For that, we declared the “str” variable of string type and assigned the input string to it. Then we have another variable, “res”, of the “int” type, and we have stored the length of the string in it. And then, we have a “for loop” so that we can print the input string characters one by one in a single line.


Here is the result of the overhead illustration:

Conclusion

In this editorial, we have come to know what the string at() method is and how we will use this method. We have also learned the writing style of the at() method and which kinds of errors and exceptions we will get through if we make logical mistakes. We have employed several illustrations to explain every line of code comprehensively. I hope you will learn a lot from this tutorial.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.