fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct stringStats
  5. {
  6. int stringLength; // total characters
  7. int upperCaseCount; // uppercase
  8. int lowerCaseCount; // lowercase
  9. int digitsCount; // digits
  10. int spacesCount; // spaces
  11. int nonAlnumCount; // non alphanumeric
  12. int vowelsCount; // vowels
  13. int nonVowelsCount; // non-vowels
  14. int specialCharsCount; // special characters are nonalphanum and nonspace
  15. int somePrintableCount; // all printable chars that are neither alphanumeric nor a space
  16. int hexadecimalCount; // 0 to 7
  17. int octalCount; // 0 to 9
  18. int binaryCount; // 0 to 1
  19. int punctuatorCount; // all printable characters that are neither alphanumeric nor a space
  20. int controlCount; // between NUL & US, and DEL
  21. int allPrintableCount; // all printable
  22.  
  23. };
  24.  
  25. // function prototype to get started, or better, use pointers */
  26. struct stringStats getStringStats (char theString[]) {
  27. struct stringStats stats = {0};
  28.  
  29. for (int i = 0; theString[i] != '\0'; i++){
  30. char c = theString[i];
  31. stats.stringLength++;
  32.  
  33. if(isupper(c))
  34. stats.upperCaseCount++;
  35. if(islower(c))
  36. stats.lowerCaseCount++;
  37. if(isdigit(c))
  38. stats.digitsCount++;
  39. if(isspace(c))
  40. stats.spacesCount++;
  41. if(!isalnum(c)) {
  42. stats.nonAlnumCount++;
  43. if(!isdigit(c)) // special characters are nonalphanum and nonspace
  44. stats.specialCharsCount++;
  45. }
  46. if(isprint(c)) {
  47. stats.allPrintableCount++;
  48. if(!isalnum(c) && !isspace(c)) {
  49. stats.somePrintableCount++; // neither alphanumeric nor a space
  50. stats.punctuatorCount++;
  51. }
  52. }
  53.  
  54. if(c >= '0' && c <= '7')
  55. stats.octalCount++;
  56.  
  57. if(c == '0' || c == '1')
  58. stats.binaryCount++;
  59.  
  60. if((c >= 0 && c <= 31) || c == 127)
  61. stats.controlCount++;
  62.  
  63. char lower = tolower(c);
  64. if (isalpha(c)) {
  65. if(lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u')
  66. stats.vowelsCount++;
  67. else
  68. stats.nonVowelsCount++;
  69. }
  70.  
  71. char upper = toupper(c);
  72. if((c >= '0' && c <= '9') || (upper >= 'A' && upper <= 'F'))
  73. stats.hexadecimalCount++;
  74.  
  75. }
  76.  
  77. return stats;
  78.  
  79. }
  80.  
  81. int main() {
  82. char inputString[500];
  83.  
  84. // Get input from the user
  85. printf("Enter a string: ");
  86. fgets(inputString, sizeof(inputString), stdin);
  87.  
  88. // Remove trailing newline character, if any
  89.  
  90. // Get the stats for the string
  91. struct stringStats stats = getStringStats(inputString);
  92.  
  93. // Print the results
  94. printf("\nString Analysis:\n");
  95. printf("Total characters: %d\n", stats.stringLength);
  96. printf("Uppercase characters: %d\n", stats.upperCaseCount);
  97. printf("Lowercase characters: %d\n", stats.lowerCaseCount);
  98. printf("Digits: %d\n", stats.digitsCount);
  99. printf("Spaces: %d\n", stats.spacesCount);
  100. printf("Non-alphanumeric characters: %d\n", stats.nonAlnumCount);
  101. printf("Vowels: %d\n", stats.vowelsCount);
  102. printf("Non-vowels: %d\n", stats.nonVowelsCount);
  103. printf("Special characters: %d\n", stats.specialCharsCount);
  104. printf("Printable characters (neither alphanumeric nor space): %d\n", stats.somePrintableCount);
  105. printf("Hexadecimal digits: %d\n", stats.hexadecimalCount);
  106. printf("Octal digits: %d\n", stats.octalCount);
  107. printf("Binary digits: %d\n", stats.binaryCount);
  108. printf("Punctuators (special printable characters): %d\n", stats.punctuatorCount);
  109. printf("Control characters: %d\n", stats.controlCount);
  110. printf("All printable characters: %d\n", stats.allPrintableCount);
  111.  
  112. return 0;
  113. }
Success #stdin #stdout 0.01s 5324KB
stdin
HELLOHIHIhi012
stdout
Enter a string: 
String Analysis:
Total characters: 14
Uppercase characters: 9
Lowercase characters: 2
Digits: 3
Spaces: 0
Non-alphanumeric characters: 0
Vowels: 5
Non-vowels: 6
Special characters: 0
Printable characters (neither alphanumeric nor space): 0
Hexadecimal digits: 4
Octal digits: 3
Binary digits: 2
Punctuators (special printable characters): 0
Control characters: 0
All printable characters: 14