fork download
  1. #include <cctype> // For character classification
  2. #include <iostream>
  3. struct StringStats {
  4. int length;
  5. int upperCase;
  6. int lowerCase;
  7. int digits;
  8. int spaces;
  9. int nonAlphaNumeric;
  10. };
  11.  
  12. StringStats analyzeString(const char* str) {
  13. StringStats stats = {0}; // Initialize all members to zero
  14.  
  15. while (*str != '\0') { // Loop until the null terminator
  16. stats.length++;
  17.  
  18. if (isupper(*str)) stats.upperCase++;
  19. else if (islower(*str)) stats.lowerCase++;
  20. else if (isdigit(*str)) stats.digits++;
  21. else if (isspace(*str)) stats.spaces++;
  22. else if (!isalnum(*str)) stats.nonAlphaNumeric++;
  23.  
  24. str++; // Move to the next character
  25. }
  26.  
  27. return stats;
  28. }
  29.  
  30.  
  31. int main() {
  32. const char* testString = "Hello World! 123 @#";
  33. StringStats stats = analyzeString(testString);
  34.  
  35. std::cout << "String Characteristics:\n"
  36. << "Length: " << stats.length << "\n"
  37. << "Uppercase: " << stats.upperCase << "\n"
  38. << "Lowercase: " << stats.lowerCase << "\n"
  39. << "Digits: " << stats.digits << "\n"
  40. << "Spaces: " << stats.spaces << "\n"
  41. << "Non-alphanumeric: " << stats.nonAlphaNumeric << "\n";
  42.  
  43. return 0;
  44. }
  45.  
  46.  
Success #stdin #stdout 0.01s 5284KB
stdin
sch
stdout
String Characteristics:
Length: 19
Uppercase: 2
Lowercase: 8
Digits: 3
Spaces: 3
Non-alphanumeric: 3