home *** CD-ROM | disk | FTP | other *** search
/ Neil's C++ Stuff / neilcpp.iso / examples / strlen.cpp < prev   
C/C++ Source or Header  |  1999-01-22  |  2KB  |  43 lines

  1. /*******************************************************************************
  2.   strlen.cpp
  3.   Neil C. Obremski
  4.   23 JAN 1999
  5.  
  6.   Uses a simple 'while' loop to count the length of the user's name (string).
  7. *******************************************************************************/
  8.  
  9. #include <iostream.h>                   // I/O stream header for 'cout'
  10.  
  11. int main()                              // declare main function
  12. {
  13.   // create variables
  14.   int i;                                // create an integer variable.  I named
  15.                                         // it 'i', because I will be using it
  16.                                         // as an index variable.
  17.   char mystring[100];                   // create a string with 99 possible
  18.                                         // characters (1 saved for the NULL
  19.                                         // character)
  20.  
  21.   // get user input
  22.   cout << "What's your name? ";         // display this string
  23.   cin >> mystring;                      // get what the user types in, into
  24.                                         // 'mystring'
  25.  
  26.   // count length of the string using a while loop
  27.   i = 0;                                // set 'i' to zero
  28.  
  29.   while (mystring[i] != 0)              // if the subscript represented by 'i'
  30.   {                                     // is not zero (not a NULL character)
  31.     i++;                                // then add one to 'i' and loop (repeat
  32.   }                                     // the process)
  33.  
  34.   // display the results
  35.   cout << "The length of your name (" << mystring << ") is " << i
  36.        << characters." << endl;
  37.  
  38.   // exit program
  39.   return 0;                             // this means its a normal termination
  40.                                         // (program ended as planned)
  41. }
  42.  
  43. /******************************* end of program *******************************/