fork download
  1. // Elaine Torrez CS1A
  2. // _____________________________________________
  3. //
  4. // MEASURE STRING LENGTH
  5. // _____________________________________________
  6. // This program prompts the user to enter a string
  7. // and determines the number of characters in the
  8. // string. The program then displays that number.
  9. // _____________________________________________
  10. // INPUT
  11. // userStr : The string entered by the user
  12. //
  13. // OUTPUT
  14. // length : Number of characters in the string
  15. // _____________________________________________
  16.  
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. // FUNCTION PROTOTYPE
  21. int strLength(const char *str);
  22.  
  23. int main()
  24. {
  25. /**********************************************
  26.   * VARIABLE DECLARATIONS
  27.   **********************************************/
  28. const int SIZE = 100;
  29. char userStr[SIZE]; // INPUT string
  30. int length; // OUTPUT string length
  31.  
  32. /**********************************************
  33.   * INPUT
  34.   **********************************************/
  35. cout << "Enter a string: ";
  36. cin.getline(userStr, SIZE);
  37.  
  38. /**********************************************
  39.   * PROCESS
  40.   **********************************************/
  41. length = strLength(userStr);
  42.  
  43. /**********************************************
  44.   * OUTPUT
  45.   **********************************************/
  46. cout << "\nString length: " << length << endl;
  47.  
  48. return 0;
  49. }
  50.  
  51. // ************************************************
  52. // strLength
  53. // ------------------------------------------------
  54. // Returns the number of characters in a C-string.
  55. // ************************************************
  56. int strLength(const char *str)
  57. {
  58. int count = 0;
  59.  
  60. while (str[count] != '\0')
  61. count++;
  62.  
  63. return count;
  64. }
  65.  
Success #stdin #stdout 0.01s 5268KB
stdin
Hello world
stdout
Enter a string: 
String length: 11