Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

10
  • 4
    @AkashSharma- You can use strlen on a C++ string by writing strlen(str.c_str()), but this would be extremely poor style. Although C++ absorbs C's standard library, the C++ standard libraries often include more C++-friendly versions of those functions, and there's no reason to use strlen when you could just be using string::length. Commented Nov 24, 2013 at 21:01
  • 4
    @AkashSharma- strlen accepts const char*, and std::string isn't a const char*. That said - it is really not a good idea to use strlen here! Commented Nov 24, 2013 at 21:19
  • 3
    @AkashSharma- I believe it's historical. The STL contains use size(), and I think historically string used length(). It's probably for backwards compatibility. Commented Nov 24, 2013 at 21:23
  • 1
    @Xlea You probably mean str[4] = '\0'? Commented Mar 22, 2016 at 10:16
  • 1
    @zenith Right, I do. Commented Mar 23, 2016 at 19:32