Determining String Lengths

Function strlen takes a string as an argument and returns the number of characters in the string—the terminating null character is not included in the length. The length is also the index of the null character. The program of Fig. 20.26 demonstrates function strlen.

 1   // Fig. 20.26: fig20_26.cpp 2   // Using strlen. 3   #include <iostream> 4   #include <cstring> // prototype for strlen  5   using namespace std; 6  7   int main() 8   { 9      const char *string1 = "abcdefghijklmnopqrstuvwxyz";10      const char *string2 = "four";11      const char *string3 = "Boston";12 13      cout << "The length of \"" << string1 << "\" is " << strlen( string1 )14         << "\nThe length of \"" << ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.