// lengthCString2A.cpp
// Finds the length of a C-string
// using a while loop.
// The C-string contains no white space.
#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 word no more than 20 characters long:> ";
char word[21]; // 20 characters plus the null character.
cin >> word; // Skips any leading whitespace, then
// reads non-whitespace characters up
// to next whitespace character.
// Because cin's extraction operator insists on
// reading at least one non-whitespace character,
// word cannot be an empty string. Its length
// must be at least 1.
// 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 word:
int length = 1;
// Check each consecutive location in word
// until a null character is found:
while ( word[length] != '\0' )
length = length + 1;
// Output the entered C-string and its length:
cout << "You typed: " << word << endl;
cout << "This word contains " << length << " characters." << endl;
return 0;
} // function main