+ 8
What is the difference between .length() and strlen() ?
2 Answers
+ 15
Now this is a good question. Assuming we have:
string str = "hello";
str.length() and strlen(str.c_str()) both returns 5.
However, as you can see, we cannot do strlen(str).
This is because strlen() only accepts const char* data type, which means that we have to convert string variable into const char* before passing it into strlen(). In the above example, I used c_str() for conversion. Function length() does not have this limitation.
0
length() is more efficient than strlen() in the terms of latency.