fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. struct stringStats
  7. {
  8. int stringLength;
  9. int upperCaseCount;
  10. int lowerCaseCount;
  11. int digitsCount;
  12. int blankSpacesCount;
  13. int nonAlphanumericCharsCount;
  14. int vowelsCount; //lower or upper case, a,e,i,o,u
  15. int nonVowelsCount; //lower or upper case
  16. int specialCharactersCount;
  17. // char* nonAlphanumericChars; // not counting spaces //malloc, pointers?, assign pointer using malloc
  18. int xDigitCount; //number of hexadecimal "digits" - 0 through 9 & the letters A - F ... accept upper and lower case
  19. int octalDigitsCount; //0-7
  20. int binaryDigitsCount; //0 or 1
  21. };
  22.  
  23. // function prototype to get started, or better, use pointers */
  24.  
  25. struct stringStats getStringStats (char theString[])
  26. {
  27. struct stringStats statsOfStr;
  28.  
  29. statsOfStr.stringLength = strlen(theString);
  30. // int *nonAlphanumericChars = malloc(statsOfStr.stringLength * sizeof(char)); //have to free memory at end of program
  31.  
  32. for (int i = 0; i < statsOfStr.stringLength; i++)
  33. {
  34. char currentChar = theString[i];
  35. if (isupper(currentChar)) {
  36. statsOfStr.upperCaseCount++;
  37. }
  38. if (islower(currentChar)) {
  39. statsOfStr.lowerCaseCount++;
  40. }
  41. if (isdigit(currentChar)) {
  42. statsOfStr.digitsCount++;
  43. }
  44. if (currentChar == ' ') {
  45. statsOfStr.blankSpacesCount++;
  46. }
  47. if (!isalnum(currentChar)) {
  48. statsOfStr.nonAlphanumericCharsCount++;
  49. // if (currentChar != ' ') {
  50. // *statsOfStr.nonAlphanumericChars = currentChar;
  51. // statsOfStr.nonAlphanumericChars++;
  52. // }
  53. }
  54. char lowerCurrentChar = tolower(currentChar);
  55. if (lowerCurrentChar == 'a' || lowerCurrentChar == 'e' || lowerCurrentChar == 'i' || lowerCurrentChar == 'o' || lowerCurrentChar == 'u') {
  56. statsOfStr.vowelsCount++;
  57. }
  58. else if (isalpha(currentChar)) {
  59. statsOfStr.nonVowelsCount++;
  60. }
  61. if (ispunct(currentChar)) {
  62. statsOfStr.specialCharactersCount++;
  63. }
  64. if (isxdigit(currentChar)) {
  65. statsOfStr.xDigitCount++;
  66. }
  67. if (currentChar >= 0 && currentChar <= 7) {
  68. statsOfStr.octalDigitsCount++;
  69. }
  70. if (currentChar == 0 || currentChar == 1) {
  71. statsOfStr.binaryDigitsCount++;
  72. }
  73. }
  74. // *statsOfStr.nonAlphanumericChars = 0; //null terminating this string
  75. //free(statsOfStr.nonAlphanumericChars); //ask papa
  76. return statsOfStr;
  77. }
  78.  
  79. int main(void) {
  80.  
  81. struct stringStats stats = getStringStats ("e ");
  82.  
  83. printf("%d\n", stats.stringLength);
  84. printf("%d\n", stats.upperCaseCount);
  85. printf("%d\n", stats.lowerCaseCount);
  86. printf("%d\n", stats.digitsCount);
  87. printf("%d\n", stats.blankSpacesCount); //messed up
  88. printf("%d\n", stats.nonAlphanumericCharsCount);
  89. printf("%d\n", stats.vowelsCount);
  90. printf("%d\n", stats.nonVowelsCount);
  91. printf("%d\n", stats.specialCharactersCount);
  92. // printf("%s\n", stats.nonAlphanumericChars);
  93. printf("%d\n", stats.xDigitCount);
  94. printf("%d\n", stats.octalDigitsCount);
  95. printf("%d\n", stats.binaryDigitsCount);
  96.  
  97. }
  98.  
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
2
1860442688
1134257345
0
1
958329249
1860442689
958386896
1134256624
1134257345
0
32764