fork download
  1. // Andres Guzman CSC5 Chapter 10, P. 588, #1
  2. /******************************************************************************
  3.  *
  4.  * DISPLAY ESSAY LENGTH
  5.  * ____________________________________________________________________________
  6.  * Program will take entered sentence within essay and display the length of
  7.  * characters, including spaces, of sentence.
  8.  *
  9.  * Computation:
  10.  *
  11.  * Program will determine the length of the string using the "strlen" command
  12.  * from the cstring header.
  13.  * ____________________________________________________________________________
  14.  *
  15.  * INPUT
  16.  *
  17.  * SIZE = Size of Array
  18.  * sentence = Entered sentence
  19.  *
  20.  * OUTPUT
  21.  *
  22.  * length = Returned value of the function
  23.  *
  24.  ******************************************************************************/
  25. #include <iostream>
  26. #include <cstring>
  27. using namespace std;
  28.  
  29. int length(char *a);
  30.  
  31. int main()
  32. {
  33. const int SIZE = 1000;
  34. char sentence[SIZE];
  35. cout << "Please Enter a Sentence: ";
  36. cin.getline(sentence, SIZE); //Gets entire line according to the size
  37. //
  38. //Outputs results with function call
  39. cout << "\nString Length: " << length(sentence) << endl;
  40. return 0;
  41. }
  42. /******************************************************************************
  43.  * Giving definition to function length. This function will add an integer value
  44.  * in accordance to the length of the c-string and to the variable length.
  45.  ******************************************************************************/
  46. int length(char *a)
  47. {
  48. int length = strlen(a); //Calculates characters before the null terminator
  49. return length; //Returns the value
  50. }
Success #stdin #stdout 0s 564KB
stdin
Hello My Good World
stdout
Please Enter a Sentence: 
String Length: 19