fork download
  1. // Saliha Babar CS1A #Chapter 10, #1, Page 588
  2. //
  3. /******************************************************************************
  4.  * CALCULATE STRING LENGTH
  5.  * ____________________________________________________________________________
  6.  * This function accepts a string up to n characters and then it calculates
  7.  * string length.
  8.  * ____________________________________________________________________________
  9.  * INPUT
  10.  * SIZE : size of the characters in string
  11.  * input : string entered by user
  12.  *
  13.  * OUTPUT
  14.  * strLength : length of the string
  15.  * ***************************************************************************/
  16. #include <iostream>
  17. #include <cstring>
  18. using namespace std;
  19. int CalcLength (char input[]);
  20.  
  21. int main() {
  22. const int SIZE = 51; // INPUT - size of the characters in string
  23. char input[SIZE]; // INPUT - array of characters
  24. int strLength; // OUTPUT - string length
  25.  
  26. cout << "Enter a word/sentences up to " << SIZE - 1 << " characters long\n";
  27. cin.getline (input, SIZE);
  28.  
  29. // Pass the input to the lengthfunc
  30. strLength = CalcLength(input);
  31.  
  32. // Display the output
  33. cout << "The string contains " << strLength << " characters.\n";
  34.  
  35. return 0;
  36. }
  37.  
  38. int CalcLength (char input[])
  39. {
  40. int length;
  41. length = strlen(input);
  42.  
  43. return length;
  44. }
Success #stdin #stdout 0s 5276KB
stdin
Saliha Babar
stdout
Enter a word/sentences up to 50 characters long
The string contains 12 characters.