fork download
  1. // **************************************************
  2. // Function: getStringStats
  3. //
  4. // Description: analyzes a string and returns counts
  5. // of different character types
  6. //
  7. // Parameters: s[] - input string
  8. //
  9. // Returns: structure containing all statistics
  10. //
  11. // **************************************************
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15.  
  16. // structure definition
  17. struct stringStats
  18. {
  19. int totalCharacters; //number of total characters in the string
  20. int upperCaseCount; // number of upper case characters
  21. int lowerCaseCount; // number of lower case characters
  22. int digitCount; // number of digits
  23. int blankSpaceCount; // number of blank spaces
  24. int nonAlnumCount; // number of non-alphanumeric characters.
  25. int vowelCount; // number of vowels (the letters a, e, i, o, u) ... accept upper and lower case
  26. int nonVowelCount; // number of non-vowels ... accept upper and lower case
  27. int specialCharCount; // number of special characters
  28. int printableNonAlnumCount; // all printable characters that are neither alphanumeric nor a space. For example ‘@’, ‘$’, etc.
  29. int hexCount; // number of hexadecimal "digits" - 0 through 9 ... and the letters A - F ... accept upper and lower case
  30. int octalCount; // number of octal "digits" - 0 to 7
  31. int binaryCount; // number of binary "digits" - 0 or 1
  32. int punctCount; // number of punctuator characters. Punctuation as defined by this function includes all printable characters that are neither alphanumeric nor a
  33. //space. For example ‘@’, ‘$’, etc
  34. int controlCount; // number of control characters, control characters are those between ASCII codes 0x00 (NUL) and 0x1f (US), plus 0x7f (DEL)
  35. int printableCount; // number of printable characters, a character is known as printable character if it occupies printing space.
  36. };
  37.  
  38. // function prototype
  39. struct stringStats getStringStats(char s[]);
  40.  
  41. int main()
  42. {
  43. char input[100];
  44. struct stringStats result;
  45.  
  46. // get input
  47. printf("Enter a string: ");
  48. fgets(input, sizeof(input), stdin);
  49.  
  50. // call function
  51. result = getStringStats(input);
  52.  
  53. // display results
  54. printf("\nTotal Characters: %d", result.totalCharacters);
  55. printf("\nUppercase: %d", result.upperCaseCount);
  56. printf("\nLowercase: %d", result.lowerCaseCount);
  57. printf("\nDigits: %d", result.digitCount);
  58. printf("\nBlank Spaces: %d", result.blankSpaceCount);
  59. printf("\nNon-Alphanumeric: %d", result.nonAlnumCount);
  60. printf("\nVowels: %d", result.vowelCount);
  61. printf("\nNon-Vowels: %d", result.nonVowelCount);
  62. printf("\nSpecial Characters: %d", result.specialCharCount);
  63. printf("\nPrintable Non-Alnum (not space): %d", result.printableNonAlnumCount);
  64. printf("\nHex Digits: %d", result.hexCount);
  65. printf("\nOctal Digits: %d", result.octalCount);
  66. printf("\nBinary Digits: %d", result.binaryCount);
  67. printf("\nPunctuation: %d", result.punctCount);
  68. printf("\nControl Characters: %d", result.controlCount);
  69. printf("\nPrintable Characters: %d\n", result.printableCount);
  70.  
  71. return 0;
  72. }
  73.  
  74. struct stringStats getStringStats(char s[])
  75. {
  76. //intialize structure
  77. struct stringStats st = {0};
  78. int i;
  79.  
  80. //loop to test each character in string
  81. for(i = 0; s[i] != '\0'; ++i)
  82. {
  83. char c = s[i];
  84. //count the total number of characters
  85. ++st.totalCharacters;
  86. //count uppercase
  87. if(isupper(c))
  88. {
  89. ++st.upperCaseCount;
  90. }//if
  91. //count lowercase
  92. if(islower(c))
  93. {
  94. ++st.lowerCaseCount;
  95. }//if
  96. //count digits
  97. if(isdigit(c))
  98. {
  99. ++st.digitCount;
  100. }//if
  101. //count spaces
  102. if(isspace(c))
  103. {
  104. ++st.blankSpaceCount;
  105. }//if
  106. //count non alphanumeric characters
  107. if(!isalnum(c)){
  108. ++st.nonAlnumCount;
  109. }//if
  110.  
  111. //count vowels and non vowels, accepts upper and lowercase
  112. if(isalpha(c))
  113. {
  114. char l = tolower(c);
  115. if(l=='a'||l=='e'||l=='i'||l=='o'||l=='u')
  116. {
  117. ++st.vowelCount;
  118. }//if
  119. else
  120. {
  121. ++st.nonVowelCount;
  122. }//else
  123. }//if
  124.  
  125. // count special characters
  126. if(ispunct(c))
  127. {
  128. ++st.specialCharCount;
  129. }//if
  130.  
  131. // counts printable but not alphanumeric and not space
  132. if(isprint(c) && !isalnum(c) && !isspace(c))
  133. {
  134. ++st.printableNonAlnumCount;
  135. ++st.punctCount;
  136. }//if
  137. //count hex digits
  138. if(isxdigit(c))
  139. {
  140. ++st.hexCount;
  141. }//if
  142. //count octal digits
  143. if(c >= '0' && c <= '7')
  144. {
  145. ++st.octalCount;
  146. }//if
  147. //count binary digits
  148. if(c == '0' || c == '1')
  149. {
  150. ++st.binaryCount;
  151. }//if
  152. //count control characters
  153. if(iscntrl(c))
  154. {
  155. ++st.controlCount;
  156. }//if
  157. //count printable characters
  158. if(isprint(c))
  159. {
  160. ++st.printableCount;
  161. }//if
  162.  
  163. }//for
  164. //return structure
  165. return st;
  166. }
Success #stdin #stdout 0s 5280KB
stdin
aBcD 1234 ?!@
stdout
Enter a string: 
Total Characters: 14
Uppercase: 2
Lowercase: 2
Digits: 4
Blank Spaces: 3
Non-Alphanumeric: 6
Vowels: 1
Non-Vowels: 3
Special Characters: 3
Printable Non-Alnum (not space): 3
Hex Digits: 8
Octal Digits: 4
Binary Digits: 1
Punctuation: 3
Control Characters: 1
Printable Characters: 13