Fundament of Strings in C++ - Algomentor

Strings in C++

Strings in c++

Hi, welcome to the series of data structure and algorithm made easy in this particular section we will about the string which is basically character array.

what is a string in C++?

A string is the stram of the character which allows a user to index every particular character the same as array allows to access its elements. example of strings are "also mentor", "developer", "coder".

How can we access all features of string in c++?

You can normally declare a string using "string" data type but if you want to access full feature of string you need to import the string library in C++.

How to import the string library in C++?

The string library can be imported as same as we import the iostream library. #include <cstring> we need to write the code in top upper lines of  C++ codes. and we are good to go.

How can we write a string in character array?

There are two ways to define a character array of string in C++.

  1. char arrchar[5] = "hello";
  2. char arrchar[5] = {'h','e','l','l','o'};
What are the function of string in C++?

The first type of function is the Iterators function they iterate through the loop from string and get the desired output for us.
  1. begin:- this return iterator to beginning.
  2. end:- this return iterator to the ending.
  3. rbegin:- this return reverse iterator to reverse beginning.
  4. rend:- this return reverse iterator to reverse ending.
  5. cbegin:- this return const iterator to beginning. 
  6. cend:- this return const iterator to the ending.
  7. crbegin:- this return reverse const iterator to reverse beginning.
  8. crend:- this return reverse const iterator to reverse ending.
The second type of function is the capacity function which deals with the capacity and size of string and string formatting.
  1. size:- return the length of the string. 
  2. length:- return the length of the string.
  3. max_size:- return the maximum size of the string data type.
  4. resize:- this function resizes the string to a particular length.
  5. capacity:- return the size of the string occupied in memory.
  6. reserve:- request to change the capacity.
  7. clear:- clear the string at the send into null.
  8. empty:- check if a string is empty or not. (true/false).
The third type of function is access function.
  1. stringname[index]:- we can directly access the particular character by using their index of the element.
  2. at:- access particular character in the string at a particular position.
  3. back:- we can access the last character of a string.
  4. front:- we can access the front character of a string.

The fourth type of function in the string library is the operator function.
  1. copy:- this function copy content of first string to second string
  2. find:- this function finds a particular character in the string.
  3. rfind:- this function finds a particular character from the last of the string and returns the position.
  4. substr:- this function can access a particular substring from a string.
  5. compare:- this function compares the two strings and returns 1,0,-1.
The fifth type of function is string modifier function
  1. '+' operator:- use to concatenate two string.
  2. append:- add a second string at the end of the first string.
  3. push_back:- append a character at the end of the string.
  4. assign:- assign a value to the string.
  5. erase:- erase the character of the string.
  6. replace:- replace the current value of the string with another string.
  7. pop_back:- delete the last character of the string.
  8. swap:- swap content of two string with each other.
  9. getline:- the help to take input on a complete string from the user.
The six types of method are converter method in these methods we convert a string of numeric value to the corresponding integer, long, and double.
  1. stoi:- convert string to an integer.
  2. stol:- string to long.
  3. stoul:- string to unsigned long.
  4. stoll:- string to long long.
  5. stoull:- string to unsigned long long.
  6. stof:- string to float.
  7. stod:- string to double.
  8. stold:- string to long double.
There are a lot of functions in the string but don't worry we will learn string with very deep we do one practical program of c++ in which we use all the functions mentioned above so that we can see how strings actually work 😀.

Here is the source code of all strings inbuilt function.

using namespace std;

int main()

string str;
// using getline to take input
cout<<"Input string is "<<str<<endl;
// using begin and end to print each charter
for ( string::iterator it=str.begin(); it!=str.end(); ++it)
    cout <<*it;
for ( string::reverse_iterator it=str.rbegin(); it!=str.rend(); ++it)
    cout <<*it;
cout<<"String size is "<<str.size()<<endl;
cout<<"String length is "<<str.length()<<endl;
cout<<"String max size is "<<str.max_size()<<endl;
cout<<"String capacity is "<<str.capacity()<<endl;
cout<<"String is empty "<<str.empty()<<endl;
// print character useing string charcter array index
for(int i=0;i<str.size();i++)
// print all character using at function
for(int i=0;i<str.size();i++)
string str2 ="the second string ";
str2="new string for new work";
cout<<"string one after swap "<<str<<endl;
cout<<"string 2 after swap "<<str2<<endl;
cout<<"sub string from index 0 to index 2 "<<str.substr(0,3)<<endl;
cout<<"comparision of two strings "<<str.compare(str2)<<endl;

        // work only if your compiler support c++11.

string num = "111213";

cout<<"string to int "<<stoi(num)<<endl;
cout<<"string to long "<<stol(num)<<endl;
cout<<"string to long long "<<stoll(num)<<endl;
cout<<"string to double "<<stod(num)<<endl;
cout<<"string to float "<<stof(num)<<endl;

return 0;

I hope you like this tutorial of string in which we demonstrate the most common and daily useable string function so you don't need to write code from scratch you just need to import string library and you are ready to solve tricky string problem.

Thank you so much for reading this article I hope you like it as well and please don't forget to share with your friends 😃.

Hi if you want to write a post on our website then please check this article on How to do a guest post.

Post a Comment