SHARE
TWEET

Character Counter

Taraxacum Oct 15th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(int argc, char const *argv[])
  6. {
  7.     int ch;
  8.     unsigned counter[256] = {0};
  9.  
  10.     while ((ch = cin.get()) > 0)
  11.     {
  12.         counter[ch]++;
  13.     }
  14.  
  15.     unsigned count_alpha = 0;
  16.     unsigned count_digit = 0;
  17.     unsigned count_space = 0;
  18.     unsigned count_other = 0;
  19.  
  20.     for (int i = 0; i < 256; i++)
  21.     {
  22.         if (isalpha(i))
  23.         {
  24.             count_alpha += counter[i];
  25.         }
  26.         else if (isdigit(i))
  27.         {
  28.             count_digit += counter[i];
  29.         }
  30.         else if (isspace(i))
  31.         {
  32.             count_space += counter[i];
  33.         }
  34.         else
  35.         {
  36.             count_space += counter[i];
  37.         }
  38.     }
  39.  
  40.     cout << "count_alpha = " << count_alpha << endl;
  41.     cout << "count_digit = " << count_digit << endl;
  42.     cout << "count_space = " << count_space << endl;
  43.     cout << "count_other = " << count_other << endl;
  44.  
  45.     return 0;
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top