cctypes.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // cctypes.cpp -- using the ctype.h library
  2. #include <iostream>
  3. #include <cctype> // prototypes for character functions
  4. int main()
  5. {
  6. using namespace std;
  7. cout << "Enter text for analysis, and type @"
  8. " to terminate input.\n";
  9. char ch;
  10. int whitespace = 0;
  11. int digits = 0;
  12. int chars = 0;
  13. int punct = 0;
  14. int others = 0;
  15. cin.get(ch); // get first character
  16. while (ch != '@') // test for sentinel
  17. {
  18. if(isalpha(ch)) // is it an alphabetic character?
  19. chars++;
  20. else if(isspace(ch)) // is it a whitespace character?
  21. whitespace++;
  22. else if(isdigit(ch)) // is it a digit?
  23. digits++;
  24. else if(ispunct(ch)) // is it punctuation?
  25. punct++;
  26. else
  27. others++;
  28. cin.get(ch); // get next character
  29. }
  30. cout << chars << " letters, "
  31. << whitespace << " whitespace, "
  32. << digits << " digits, "
  33. << punct << " punctuations, "
  34. << others << " others.\n";
  35. // cin.get();
  36. // cin.get();
  37. return 0;
  38. }