fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 10 P.588 #1
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute String Length
  6.  * ____________________________________________________________________________
  7.  * This program will accept a string from the user and calculate the amount of
  8.  * characters within that string.
  9.  * ____________________________________________________________________________
  10.  * Input
  11.  * userInput :The string the user decideds to enter into the program
  12.  * countChars :The function that counts the amount of characters
  13.  * Output
  14.  * length :The total amount of characters in the string the user inputted
  15.  *****************************************************************************/
  16. #include <iostream>
  17. #include <iomanip>
  18. using namespace std;
  19.  
  20. //Function Prototype
  21. int countChars(const char *str);
  22.  
  23. int main() {
  24. //Data Dictionary
  25. string userInput;
  26. int length;
  27.  
  28. //User Input
  29. cout << "Enter a string: " << endl;
  30. getline(cin, userInput);
  31.  
  32. length = countChars(userInput.c_str()); //Converst String to C-String
  33.  
  34. //Display!
  35. cout << "The number of characters in the string is: " << length << endl;
  36. return 0;
  37. }
  38. //Function Definition
  39. int countChars(const char *str){
  40. int count = 0;
  41. while(str[count] != '\0'){
  42. count ++;
  43. }
  44. return count;
  45. }
Success #stdin #stdout 0.01s 5300KB
stdin
charlotte
stdout
Enter a string: 
The number of characters in the string is: 9