fork 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
  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. struct stringStats getStringStats (char theString[])
  24. {
  25. struct stringStats statsOfStr = {0,0,0,0,0,0,0,0,0,0,0,0,0};
  26.  
  27. statsOfStr.stringLength = strlen(theString);
  28. statsOfStr.nonAlphanumericChars = malloc(statsOfStr.stringLength * sizeof(char)); //have to free memory at end of program
  29. char * nonAlphanumericCharsPtr = statsOfStr.nonAlphanumericChars ; // current ptr
  30.  
  31. for (int i = 0; i < statsOfStr.stringLength; i++)
  32. {
  33. char currentChar = theString[i];
  34. if (isupper(currentChar)) {
  35. statsOfStr.upperCaseCount++;
  36. }
  37. if (islower(currentChar)) {
  38. statsOfStr.lowerCaseCount++;
  39. }
  40. if (isdigit(currentChar)) {
  41. statsOfStr.digitsCount++;
  42. }
  43. if (currentChar == ' ') {
  44. statsOfStr.blankSpacesCount++;
  45. }
  46. if ( !isalnum(currentChar) ) {
  47. statsOfStr.nonAlphanumericCharsCount++;
  48. if (currentChar != ' ' && isprint(currentChar) ) {
  49. *nonAlphanumericCharsPtr = currentChar;
  50. nonAlphanumericCharsPtr++;
  51. }
  52. }
  53. char lowerCurrentChar = tolower(currentChar);
  54. if (lowerCurrentChar == 'a' || lowerCurrentChar == 'e' || lowerCurrentChar == 'i' || lowerCurrentChar == 'o' || lowerCurrentChar == 'u') {
  55. statsOfStr.vowelsCount++;
  56. }
  57. else if (isalpha(currentChar)) {
  58. statsOfStr.nonVowelsCount++;
  59. }
  60. if (ispunct(currentChar)) {
  61. statsOfStr.specialCharactersCount++;
  62. }
  63. if (isxdigit(currentChar)) {
  64. statsOfStr.xDigitCount++;
  65. }
  66. if (currentChar >= '0' && currentChar <= '7') {
  67. statsOfStr.octalDigitsCount++;
  68. }
  69. if (currentChar == '0' || currentChar == '1') {
  70. statsOfStr.binaryDigitsCount++;
  71. }
  72. }
  73. *nonAlphanumericCharsPtr = 0; //null terminating this string
  74. //free(statsOfStr.nonAlphanumericChars); should be called by the calling function when nonAlphanumericChars no longer needed
  75. return statsOfStr;
  76. }
  77.  
  78. int main(void) {
  79.  
  80. struct stringStats stats = getStringStats ("A5$0@");
  81.  
  82. printf("%d\n", stats.stringLength);
  83. printf("%d\n", stats.upperCaseCount);
  84. printf("%d\n", stats.lowerCaseCount);
  85. printf("%d\n", stats.digitsCount);
  86. printf("%d\n", stats.blankSpacesCount);
  87. printf("%d\n", stats.nonAlphanumericCharsCount);
  88. printf("%d\n", stats.vowelsCount);
  89. printf("%d\n", stats.nonVowelsCount);
  90. printf("%d\n", stats.specialCharactersCount);
  91. printf("%s\n", stats.nonAlphanumericChars);
  92. printf("%d\n", stats.xDigitCount);
  93. printf("%d\n", stats.octalDigitsCount);
  94. printf("%d\n", stats.binaryDigitsCount);
  95. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
5
1
0
2
0
2
1
0
2
$@
3
2
1