[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppStrlen
It is similar to the std::string::size method of a std::string. The code below shows this:
Use std::string instead of an array of char [1,2].
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppStrlen
(C++) std::strlen
Standard function to get the length of a char* (a C-style or null-terminated string) not including the \0 terminator.int main() { const char * const myString = "Bilderbikkel"; const int length = std::strlen(myString); std::cout << "Length is " << length << std::endl; }
- include <cstring>
- include <iostream>
It is similar to the std::string::size method of a std::string. The code below shows this:
int main() { const std::string myString = "Bilderbikkel"; const int lengthCppStyle = myString.size(); const int lengthCstyle = std::strlen(myString.c_str()); assert(lengthCstyle == lengthCppStyle); }
- include <cstring>
- include <string>
- include <cassert>
Use std::string instead of an array of char [1,2].
Code links
- #include
- char
- const
- cout, std::cout
- cstring (header file)
- endl, std::endl
- include, #include
- int
- iostream (header file)
- main
- std::cout
- std::endl
Reference
- 1) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4 Chapter 5.8.5: 'Use string rather then zero-terminated arrays of char'.
- 2) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 77: 'Use vector and string instead of arrays.'
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
