c++ string length() function, helps to find length of string c++ and return an integer value as a byte. It is array of characters that ends with a null character.
Methods
This method can be used to find length of string c++:
std::string methods
C library function
Loops function
Syntax
syntax for length of string c++
string.length();
c++ string size
Some coder use size() function to get string length c++
C++ string length vs c++ string size
C++ String Length() and c++ string size() functions are the same, just synonyms with each other.
Example
In this tutorial, we will learn how to find length of string c++ and c++ string size
we have used CodeBlocks compiler for debugging purposes. But you can use any C++ programming language compiler as per your availability.
Example 1:
In this example, we ask users to write a maximum length of string c++ of 100 characters.
#include<iostream>#include<math.h>usingnamespacestd;
//function declarationintstringLength(char *);
intmain(){
char text[100];
int length;
cout<<"Enter text (max- 100 characters): ";
cin>>text;
length = stringLength(text);
cout<<"Input text is: "<<text<<endl;
cout<<"Length is: "<<length<<endl;
return0;
}
intstringLength(char *str){
int len=0;
//calculating string lengthfor(len=0; str[len]!='\0'; len++);
//returning lenreturn len;
}
length of string c++ and c++ string size
The function “stringLength” is used to find the length of input and return both input and length as a result.
Result
The program gets executed and the function returns “0” as result.