// lengthCString2B.cpp
// Finds the length of a C-string
// using a while loop.
// The C-string is a line of text
// and may contain whitespace.
#include <iostream>
using namespace std;
int main()
{
// Announce purpose of program:
cout << "This program finds the length of a C-string "
<< "entered by the user." << endl << endl;
// Input a C-string:
cout << "Enter a line no more than 20 characters long:> ";
char line[21]; // 20 characters plus the null character
cin.getline(line, 21); // Reads a line of text,
// including whitespace
// If the user pressed [Enter] without entering
// any characters, line will be an empty string.
// Compute the length of the entered C-string
// by searching for the null character:
// Prepare to search for a null character at the
// first possible location in line:
int length = 0;
// Check each consecutive location in line
// until a null character is found:
while ( line[length] != '\0' )
length = length + 1;
// Output the entered C-string and its length:
cout << "You typed: " << line << endl;
cout << "This line contains " << length << " characters." << endl;
return 0;
} // function main