// lengthCString1B.cpp
// Finds the length of a C-string
// using nested if/else.
// 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:
int length;
if ( line[0] == '\0' )
length = 0;
else if ( line[1] == '\0' )
length = 1;
else if ( line[2] == '\0' )
length = 2;
else if ( line[3] == '\0' )
length = 3;
else if ( line[4] == '\0' )
length = 4;
else if ( line[5] == '\0' )
length = 5;
else if ( line[6] == '\0' )
length = 6;
else if ( line[7] == '\0' )
length = 7;
else if ( line[8] == '\0' )
length = 8;
else if ( line[9] == '\0' )
length = 9;
else if ( line[10] == '\0' )
length = 10;
else if ( line[11] == '\0' )
length = 11;
else if ( line[12] == '\0' )
length = 12;
else if ( line[13] == '\0' )
length = 13;
else if ( line[14] == '\0' )
length = 14;
else if ( line[15] == '\0' )
length = 15;
else if ( line[16] == '\0' )
length = 16;
else if ( line[17] == '\0' )
length = 17;
else if ( line[18] == '\0' )
length = 18;
else if ( line[19] == '\0' )
length = 19;
else // if ( line[20] == '\0' )
length = 20;
// Output the entered C-string and its length:
cout << "You typed: " << line << endl;
cout << "This line contains " << length << " characters." << endl;
return 0;
} // function main