home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- strlen.cpp
- Neil C. Obremski
- 23 JAN 1999
-
- Uses a simple 'while' loop to count the length of the user's name (string).
- *******************************************************************************/
-
- #include <iostream.h> // I/O stream header for 'cout'
-
- int main() // declare main function
- {
- // create variables
- int i; // create an integer variable. I named
- // it 'i', because I will be using it
- // as an index variable.
- char mystring[100]; // create a string with 99 possible
- // characters (1 saved for the NULL
- // character)
-
- // get user input
- cout << "What's your name? "; // display this string
- cin >> mystring; // get what the user types in, into
- // 'mystring'
-
- // count length of the string using a while loop
- i = 0; // set 'i' to zero
-
- while (mystring[i] != 0) // if the subscript represented by 'i'
- { // is not zero (not a NULL character)
- i++; // then add one to 'i' and loop (repeat
- } // the process)
-
- // display the results
- cout << "The length of your name (" << mystring << ") is " << i
- << characters." << endl;
-
- // exit program
- return 0; // this means its a normal termination
- // (program ended as planned)
- }
-
- /******************************* end of program *******************************/