fork download
  1. //Maxwell Brewer CS1A Chapter 10, p. 591, #15
  2. //
  3. /*******************************************************************************
  4.  * STATISTICAL ANALYSIS OF TEXT FILE CHARACTERS
  5.  * (MODIFIED FOR USER INPUT INSTEAD OF READING FROM A FILE)
  6.  * _____________________________________________________________________________
  7.  * This program reads a string entered by the user and performs a statistical
  8.  * analysis of its content. Specifically, it calculates the number of
  9.  * uppercase letters, lowercase letters, and numeric digits in the input.
  10.  *
  11.  * INPUT:
  12.  * userInput : A string entered by the user.
  13.  *
  14.  * OUTPUT:
  15.  *
  16.  * : The total count of uppercase letters.
  17.  * : The total count of lowercase letters.
  18.  * : The total count of numeric digits.
  19.  *
  20.  ******************************************************************************/
  21.  
  22. #include <iostream>
  23. #include <string>
  24.  
  25. using namespace std;
  26.  
  27. // Function prototype
  28. void Cal_Statistics(const string &input, unsigned &uppercase,
  29. unsigned &lowercase, unsigned &digit);
  30.  
  31. int main() {
  32. // Variables to hold statistics
  33. unsigned uppercase = 0, lowercase = 0, digit = 0;
  34.  
  35. // Variable to hold user input
  36. string userInput;
  37.  
  38. cout << "==============================================\n";
  39.  
  40. // Prompt the user to enter a string
  41. cout << "Please enter a string:\n";
  42. getline(cin, userInput);
  43.  
  44. // Call the function to calculate statistics
  45. Cal_Statistics(userInput, uppercase, lowercase, digit);
  46.  
  47. // Display the calculated statistics
  48. cout << "\n\t\t:: Results ::\n";
  49. cout << "Uppercases : " << uppercase << "\n";
  50. cout << "Lowercases : " << lowercase << "\n";
  51. cout << "Digits : " << digit << "\n";
  52.  
  53. return 0;
  54. }
  55.  
  56. // Function to calculate statistics
  57. void Cal_Statistics(const string &input, unsigned &uppercase,
  58. unsigned &lowercase, unsigned &digit) {
  59.  
  60. for (char ch : input) {
  61. if (isupper(ch)) {
  62. uppercase++;
  63. } else if (islower(ch)) {
  64. lowercase++;
  65. } else if (isdigit(ch)) {
  66. digit++;
  67. }
  68. }
  69. }
Success #stdin #stdout 0.01s 5288KB
stdin
esvtvw57tvw897w3847t9vw3879vTFtedYGJHEDswhbHjhd778
stdout
==============================================
Please enter a string:

		:: Results ::
Uppercases : 9
Lowercases : 24
Digits     : 17