What is the difference between strlen() function & str_name.length() function?
According to me both gives the length of the string.
Please explain the difference between these two in detail with example😊
What is the difference between strlen() function & str_name.length() function?
According to me both gives the length of the string.
Please explain the difference between these two in detail with example😊
strlen() is used to find the length of only character array
whereas length() is member function of string
when you decalre string str;
then only you can use str.length();
because it is member function of string class
strlen() is find in cstring file
whereas str_name.length() is present in iostream file
#include <iostream>
#include<cstring>
using namespace std;
int main() {
char s[100];
cin>>s;
cout<<strlen(s);
string s2;
cin>>s2;
cout<<s2.length();
}
Your code is ghowing errow in many lines. Please try to run in online IDE
Also I want to knnw that what is the difference between strlen() & sizeof() ?
Correction to previous comment “Know”
there is no error in the code i have pasted it from ide
code link
work of strlen() , string::length() looks similar
but they are used in different situations
strlen used for char arrays
string::length() used for strings
sizeof is completely different thing
The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.
The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.
The syntax of using sizeof is as follows −
sizeof (data type)
okky thank you Saurabh for explaining!!