fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct
  4. {
  5. int total_chars;
  6. int upper_case;
  7. int lower_case;
  8. int digits;
  9. int spaces;
  10. int non_alnum;
  11. int vowels;
  12. int non_vowels;
  13. int special_chars;
  14. char special_printables[128];
  15. int hex_digits;
  16. int octal_digits;
  17. int binary_digits;
  18. } stringStats;
  19.  
  20. // **********************************************************************************************
  21. // Function: blackJackValue
  22. //
  23. // Description: gets the value of 2 given cards
  24. //
  25. // Parameters: card1 - char of a card, from 2-9, T, K, Q, J for 10, and A for 11
  26. // card2 - same values as card1
  27. //
  28. // Returns: total - int value of both cards, unless 2 A values, in which the total will be 12,
  29. // returns -1 if invalid input
  30. //
  31. // **********************************************************************************************
  32. stringStats getStringStats(const char *str)
  33. {
  34. stringStats result = {0}; //set everything to 0
  35. int specialIndex = 0;
  36.  
  37. for (int i = 0; str[i] != '\0'; i++)
  38. {
  39. //all credit goes to the C standard library for making this section much easier
  40. char ch = str[i];
  41. result.total_chars++;
  42. if (isupper(ch)) result.upper_case++;
  43. if (islower(ch)) result.lower_case++;
  44. if (isdigit(ch)) result.digits++;
  45. if (isspace(ch)) result.spaces++;
  46. if (!isalnum(ch))
  47. {
  48. result.non_alnum++;
  49. if (isprint(ch) && !isspace(ch))
  50. {
  51. result.special_chars++;
  52. result.special_printables[specialIndex++] = ch;
  53. }
  54. } //checks if a number is not alphanumeric, then if its printable or a space, if not, then increments the amount of special chars, and adds the char to the special printables
  55. if (strchr("AEIOUaeiou", ch)) result.vowels++; //finally caved and used a better way of finding specific chars
  56. else if (isalpha(ch)) result.non_vowels++;
  57. if ((ch >= '0' && ch <= '9') || (toupper(ch) >= 'A' && toupper(ch) <= 'F')) result.hex_digits++;
  58. if (ch >= '0' && ch <= '7') result.octal_digits++;
  59. if (ch == '0' || ch == '1') result.binary_digits++; //checks for hex, octal, and binary
  60. }
  61.  
  62.  
  63. return result;
  64. }
  65.  
  66. int main() {
  67. const char *testStr = "Hello World! 123 ABC @#$%@#$% 0xF7";
  68. stringStats stats = getStringStats(testStr);
  69.  
  70. printf("Total characters: %d\n", stats.total_chars);
  71. printf("Uppercase characters: %d\n", stats.upper_case);
  72. printf("Lowercase characters: %d\n", stats.lower_case);
  73. printf("Digits: %d\n", stats.digits);
  74. printf("Spaces: %d\n", stats.spaces);
  75. printf("Non-alphanumeric characters: %d\n", stats.non_alnum);
  76. printf("Vowels: %d\n", stats.vowels);
  77. printf("Non-vowels: %d\n", stats.non_vowels);
  78. printf("Special characters: %d\n", stats.special_chars);
  79. printf("Special printable characters: %s\n", stats.special_printables);
  80. printf("Hexadecimal digits: %d\n", stats.hex_digits);
  81. printf("Octal digits: %d\n", stats.octal_digits);
  82. printf("Binary digits: %d\n", stats.binary_digits);
  83.  
  84. return 0;
  85. }
  86.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Total characters: 34
Uppercase characters: 6
Lowercase characters: 9
Digits: 5
Spaces: 5
Non-alphanumeric characters: 14
Vowels: 4
Non-vowels: 11
Special characters: 9
Special printable characters: !@#$%@#$%
Hexadecimal digits: 11
Octal digits: 5
Binary digits: 2