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         << " The length of "" << string2 << "" is " << strlen( string2 )
15         << " The length of "" << string3 << "" is " << strlen( string3 )
16         << endl;
17   } // end main


The length of "abcdefghijklmnopqrstuvwxyz" is 26
The length of "four" is 4
The length of "Boston" is 6


Fig. 20.26. strlen returns the length of a char * string.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.195.236