fork download
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. struct stringStats
  6. {
  7. int stringLength;
  8. int upperCaseCount;
  9. int lowerCaseCount;
  10. int digitCount;
  11. int spaceCount;
  12. int nonAlphaCount;
  13. int vowelCount;
  14. int specialCount;
  15. int otherCount;
  16. int hexCount;
  17. int octalCount;
  18. int binaryCount;
  19. int punctuatorCount;
  20. int controlCount;
  21. int printableCount;
  22.  
  23. };
  24.  
  25. // function prototype to get started, or better, use pointers */
  26. struct stringStats getStringStats (char theString[]){
  27. //create structure and initialize all values to zero
  28. struct stringStats stats;
  29. stats.stringLength = strlen(theString);
  30. stats.upperCaseCount = 0;
  31. stats.lowerCaseCount = 0;
  32. stats.digitCount = 0;
  33. stats.spaceCount = 0;
  34. stats.nonAlphaCount = 0;
  35. stats.vowelCount = 0;
  36. stats.specialCount = 0;
  37. stats.otherCount = 0;
  38. stats.hexCount = 0;
  39. stats.octalCount = 0;
  40. stats.binaryCount = 0;
  41. stats.punctuatorCount = 0;
  42. stats.controlCount = 0;
  43. stats.printableCount = 0;
  44.  
  45. int i;
  46. //loop through the string and increment each stat as
  47. for (i=1; i<strlen(theString); i++){
  48. //!! that I use here is a little cursed - I'm using it to invert things twice.
  49. //I expected these functions to return 0 or 1, but many of them return
  50. //a random positive integer - because I want to use them to increment my
  51. //counters, this turns that into 0/1.
  52. char c = theString[i];
  53. char cl = isupper(c) ? tolower(c) : c;
  54. stats.upperCaseCount += !!isupper(c);
  55. stats.lowerCaseCount += !!islower(c);
  56. stats.digitCount += !!isdigit(c);
  57. stats.spaceCount += !!isblank(c);
  58. stats.nonAlphaCount += !isalnum(c);
  59. stats.vowelCount += (cl=='a'||cl=='e'||cl=='i'||cl=='o'||cl=='u'||cl=='y');
  60. //I couldn't really determine what was meant by a special character.
  61. //the best definition I could find was just characters that weren't alphanumeric or spaces,
  62. //but that shares the same definition as 'other' and 'punctuator' which feels a bit weird.
  63. stats.specialCount = (isprint(c) && !(isalpha(c) || isspace(c)));
  64. stats.otherCount = (isprint(c) && !(isalpha(c) || isspace(c)));
  65. stats.hexCount += !!isxdigit(c);
  66. stats.octalCount += (c>47&&c<57); //check to see if it's in the 1-8 range
  67. stats.binaryCount += (c == '0'||c=='1');
  68. stats.punctuatorCount += (isprint(c) && !(isalpha(c) || isspace(c)));
  69. stats.controlCount = !!iscntrl(c);
  70. stats.printableCount = !!isprint(c);
  71. }
  72. return stats;
  73.  
  74. }
  75.  
  76. int main(void) {
  77. // your code goes here
  78.  
  79. struct stringStats stats = getStringStats("potato12345671234");
  80. printf("\n%d", stats.octalCount);
  81. }
  82.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
11