Introduction to string and functions:

C++ Standard template library includes a header file called “string”. 

The string header file provides us a data type called “string” with a variety of functions to make our life easier.


string <string_name>;

We can access each element of the string just like we access our character array strings(str[index]).

But they offer us a lot of advantages over the traditional character arrays, for instance, the string length function which is implemented in the given code.


  1. First, we will input a string from the user(whose length is to be found) and store it in a string variable s.
  2. Then we will print the length of the string using the length function(s.length()).


#include <iostream>
#include <string>  //for using string data type
#include <cstdio>  //for using getline function to input string
using namespace std;

int main(){
	string s;
	cout << "Enter a string\n";
	getline(cin, s);  //taking input in the string
	cout << "Length of the given string=" << s.length();

Report Error/ Suggestion

Related Posts:

CopyRight © 2020

CopyRight © 2020