Sclafus

Uppercase percent per line from file

Nov 29th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int uppercase_in_text(string testo) {
  6.     int uppercase_counter = 0;
  7.  
  8.     for (char carattere_testo : testo){
  9.         if (isupper(carattere_testo)){
  10.             uppercase_counter++;
  11.         }
  12.     }
  13.  
  14.     return uppercase_counter;
  15. }
  16.  
  17.  
  18. int main(){
  19.     int sum_uppercase = 0, char_in_file = 0;
  20.     ifstream file1{"input_9.1.txt"};
  21.  
  22.     for (string line; getline(file1, line);){
  23.         char_in_file += sizeof(line);
  24.         int upper_in_line = uppercase_in_text(line);
  25.         sum_uppercase += upper_in_line;
  26.         cout << "Uppercase percent for this line: " << float(upper_in_line) / sizeof(line) * 100 << "%" << endl;
  27.     }
  28.     cout << "Uppercase percent for the whole text: " << float(sum_uppercase) / char_in_file * 100 << "%" << endl;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment