fork download
  1. #include <stdio.h>
  2. #include <ctype.h> // For character type functions
  3. #include <cstring> // For strlen function
  4.  
  5. // Define the structure to hold string statistics
  6. struct stringStats {
  7. int stringLength;
  8. int upperCaseCount;
  9. int lowerCaseCount;
  10. int digitCount;
  11. int spaceCount;
  12. int nonAlphanumericCount;
  13. };
  14.  
  15. // Function prototype
  16. struct stringStats getStringStats(const char *theString);
  17.  
  18. int main() {
  19. char inputString[100]; // Input string buffer
  20.  
  21. printf("Enter a string: ");
  22. if (fgets(inputString, sizeof(inputString), stdin) == NULL) {
  23. printf("Error reading input.\n");
  24. return 1; // Return an error code if fgets fails
  25. }
  26.  
  27. // Handling the newline character fgets reads
  28. size_t ln = strlen(inputString) - 1;
  29. if (inputString[ln] == '\n')
  30. inputString[ln] = '\0';
  31.  
  32. // Get statistics of the string
  33. struct stringStats stats = getStringStats(inputString);
  34.  
  35. // Print the statistics
  36. printf("String Length: %d\n", stats.stringLength);
  37. printf("Upper Case Letters: %d\n", stats.upperCaseCount);
  38. printf("Lower Case Letters: %d\n", stats.lowerCaseCount);
  39. printf("Digits: %d\n", stats.digitCount);
  40. printf("Blank Spaces: %d\n", stats.spaceCount);
  41. printf("Non-Alphanumeric Characters: %d\n", stats.nonAlphanumericCount);
  42.  
  43. return 0;
  44. }
  45.  
  46. // Function to analyze the string and return its statistics
  47. struct stringStats getStringStats(const char *theString) {
  48. struct stringStats stats = {0}; // Initialize all fields to zero
  49.  
  50. const char *p = theString; // Pointer to iterate through the string
  51.  
  52. while (*p) { // Continue until the end of the string
  53. stats.stringLength++; // Increment string length for each character
  54.  
  55. if (isupper(*p)) {
  56. stats.upperCaseCount++;
  57. } else if (islower(*p)) {
  58. stats.lowerCaseCount++;
  59. } else if (isdigit(*p)) {
  60. stats.digitCount++;
  61. } else if (isspace(*p)) {
  62. stats.spaceCount++;
  63. } else {
  64. stats.nonAlphanumericCount++;
  65. }
  66.  
  67. p++; // Move to the next character
  68. }
  69.  
  70. return stats;
  71. }
Success #stdin #stdout 0.01s 5292KB
stdin
Hello World! 1234 Testing, testing...!
stdout
Enter a string: String Length: 38
Upper Case Letters: 3
Lower Case Letters: 21
Digits: 4
Blank Spaces: 4
Non-Alphanumeric Characters: 6