...

/

Built-in Functions for Strings

Built-in Functions for Strings

Learn to use popular built-in functions related to strings in C++.

C++ provides various built-in functions for dealing with strings. These functions are collectively called built-in string functions.

There are numerous tasks related to strings that make it easy for programmers to perform routine tasks. This section will provide examples and results of some commonly used string functions.

Obtaining the string info

The length() function is used to find the number of characters or the length of a string. The functions, begin() and end(), are used to find the start and the end of a string.

Remember: We need to write #include <string> at the start of the program to use these functions.

The following program demonstrates the use of length() function:

Press + to interact
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Educative";
int len = str.length();
cout << "Length of the string 'Educative' is: " << len << endl;
return 0;
}

In the program above:

  • We declare a string variable str and assign a string Educative to it.
  • We call the built-in length() function and store the returned value in an int variable.
  • We display the length of the string using the cout statement.
...