fork download
  1. // **************************************************
  2. // Function: getStringStats
  3. //
  4. // Description: analyzes a string and counts different
  5. // types of characters
  6. //
  7. // Parameters: s[] - input string
  8. //
  9. // Returns: structure containing all counts
  10. //
  11. // **************************************************
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15.  
  16. // structure definition
  17. struct stringStats
  18. {
  19. int stringLength;
  20. int upperCaseCount;
  21. int lowerCaseCount;
  22. int digitCount;
  23. int spaceCount;
  24. int nonAlnumCount;
  25. int vowelCount;
  26. int nonVowelCount;
  27. int specialCharCount;
  28. int symbolCount; //placeholder name
  29. int hexCount;
  30. int octalCount;
  31. int binaryCount;
  32. int punctCount;
  33. int controlCount;
  34. int printableCount;
  35. };
  36.  
  37. // function prototype
  38. struct stringStats getStringStats(char s[]);
  39.  
  40. int main()
  41. {
  42. char input[100];
  43. struct stringStats result;
  44.  
  45. // get input from user
  46. printf("Enter a string: ");
  47. fgets(input, sizeof(input), stdin);
  48.  
  49. // call function
  50. result = getStringStats(input);
  51.  
  52. // display results
  53. printf("\nString Length: %d", result.stringLength);
  54. printf("\nUppercase: %d", result.upperCaseCount);
  55. printf("\nLowercase: %d", result.lowerCaseCount);
  56. printf("\nDigits: %d", result.digitCount);
  57. printf("\nSpaces: %d", result.spaceCount);
  58. printf("\nNon-Alphanumeric: %d", result.nonAlnumCount);
  59. printf("\nControl Characters: %d", result.controlCount);
  60. printf("\nPrintable Characters: %d", result.printableCount);
  61. printf("\nVowels: %d", result.vowelCount);
  62. printf("\nNon-Vowels: %d", result.nonVowelCount);
  63. printf("\nHex Digits: %d", result.hexCount);
  64. printf("\nOctal Digits: %d", result.octalCount);
  65. printf("\nBinary Digits: %d", result.binaryCount);
  66. printf("\nPunctuation: %d", result.punctCount);
  67. printf("\nSpecial Characters: %d\n", result.specialCharCount);
  68.  
  69. return 0;
  70. }
  71.  
  72. struct stringStats getStringStats(char s[])
  73. {
  74. struct stringStats st = {0};
  75. int i = 0;
  76.  
  77. while(s[i] != '\0')
  78. {
  79. char c = s[i];
  80. st.stringLength++;
  81.  
  82. if(isupper(c)) st.upperCaseCount++;
  83. if(islower(c)) st.lowerCaseCount++;
  84. if(isdigit(c)) st.digitCount++;
  85. if(isspace(c)) st.spaceCount++;
  86. if(!isalnum(c)) st.nonAlnumCount++;
  87. if(iscntrl(c)) st.controlCount++;
  88. if(isprint(c)) st.printableCount++;
  89.  
  90. if(isalpha(c))
  91. {
  92. char l = tolower(c);
  93. if(l=='a'||l=='e'||l=='i'||l=='o'||l=='u')
  94. st.vowelCount++;
  95. else
  96. st.nonVowelCount++;
  97. }
  98.  
  99. if(isxdigit(c)) st.hexCount++;
  100. if(c >= '0' && c <= '7') st.octalCount++;
  101. if(c == '0' || c == '1') st.binaryCount++;
  102. if(ispunct(c)) st.punctCount++;
  103. if(ispunct(c)) st.specialCharCount++;
  104.  
  105. i++;
  106. }
  107.  
  108. return st;
  109. }
  110.  
Success #stdin #stdout 0s 5296KB
stdin
AsdfghBB  !!2345
stdout
Enter a string: 
String Length: 16
Uppercase: 3
Lowercase: 5
Digits: 4
Spaces: 2
Non-Alphanumeric: 4
Control Characters: 0
Printable Characters: 16
Vowels: 1
Non-Vowels: 7
Hex Digits: 9
Octal Digits: 4
Binary Digits: 0
Punctuation: 2
Special Characters: 2