fork download
  1. //Sam Trivikraman CS1A Chapter 10, p. 588, #1
  2. //
  3. /*
  4.  ******************************************************************************
  5. Calculate the length of a string
  6. _______________________________________________________________________________
  7. This program counts and displays the length of a string.
  8. _______________________________________________________________________________
  9. INPUT
  10. the string of words : The user inputted string
  11.  
  12. OUTPUT
  13. string length : The length of the string
  14. _______________________________________________________________________________
  15. *******************************************************************************
  16. */
  17. #include <iostream>
  18. #include <cstring>
  19. using namespace std;
  20.  
  21. //function that calculates the length of the string
  22. int length(string str)
  23. {
  24. int len; //OUTPUT The length of the string
  25. //Calculate the length of the string
  26. len = str.length();
  27. //return the length of the string
  28. return len;
  29. }
  30.  
  31. int main() {
  32.  
  33. string word; //INPUT The user inputted string
  34.  
  35. //ask the user for a string
  36. cout << "Please enter a string. " << endl;
  37. cin >> word;
  38. //Call the string length function and output the length of the string
  39. cout << "The length of the string is: " << length(word) << endl;
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5300KB
stdin
hello
stdout
Please enter a string. 
The length of the string is: 5