Sclafus

Contatore di 1 e 0 nel testo

Nov 29th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int contatore(char n){
  7.     int sentinella0 = 0;
  8.     int sentinella1 = 0;
  9.  
  10.  
  11.         if (n == '0'){
  12.             sentinella0++;
  13.         }
  14.         if (n == '1'){
  15.             sentinella1++;
  16.         }
  17.  
  18.  
  19.     return sentinella0, sentinella1;
  20. }
  21.  
  22. //La sentinella0 da valori casuali
  23. int main()
  24. {
  25.     string testo = "";
  26.     int lunghezza_testo;
  27.     cout << "Inserire il testo da analizzare" << endl;
  28.     getline(cin, testo);
  29.     lunghezza_testo = testo.size();
  30.     cout << "Il testo รจ lungo " << lunghezza_testo << endl;
  31.     int sum_0, sum_1 = 0;
  32.     for (char n : testo)
  33.     {
  34.         int temp0, temp1 = 0;
  35.         temp0, temp1 = contatore(n);
  36.         sum_0 += temp0;
  37.         sum_1 += temp1;
  38.     }
  39.     vector<int> results = {sum_0, sum_1};
  40.     for (auto elemento : results)
  41.     {
  42.         cout << elemento << endl;
  43.     }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment