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