fork download
  1. //Sam Partovi CS1A Ch. 10, P.588, #1
  2. /*******************************************************************************
  3. * DETERMINE STRING LENGTH
  4. * ____________________________________________________________
  5. * This program accepts a given string and determines the length, in characters.
  6. * The program then returns the number of characters present in the string.
  7. * ____________________________________________________________
  8. * INPUT
  9. * ARRAY_SIZE: Size of string array
  10. * string: Given string to determine length
  11. * OUTPUT
  12. * stringLength: Length of given string in characters
  13. *******************************************************************************/
  14. #include <iostream>
  15. #include <cstring>
  16. using namespace std;
  17.  
  18. //Function prototype for DetermineLength
  19. int DetermineLength(char string[]);
  20.  
  21. int main() {
  22.  
  23. const int ARRAY_SIZE = 51; //INPUT - Size of string array
  24. char string[ARRAY_SIZE]; //INPUT - Given string to determine length
  25. int stringLength; //OUTPUT - Length of given string in characters
  26.  
  27. //Prompt for given string
  28. cout << "Enter a string up to " << ARRAY_SIZE - 1 << " characters: ";
  29. cin.getline(string, ARRAY_SIZE);
  30.  
  31. //Call DetermineLength function and assign returned value to stringLength
  32. stringLength = DetermineLength(string);
  33.  
  34. //Display the result of string length
  35. cout << "\nThe string is " << stringLength << " characters long.";
  36.  
  37. return 0;
  38. }
  39.  
  40. //******************************************************************************
  41. //Function definition for DetermineLength:
  42. // This function accepts a given string and returns the value of the string
  43. // length, in characters.
  44. //*****************************************************************************/
  45. int DetermineLength(char string[]) {
  46. return strlen(string);
  47. }
  48.  
Success #stdin #stdout 0s 5268KB
stdin
The sky is blue and the grass is green
stdout
Enter a string up to 50 characters: 
The string is 38 characters long.