fork download
  1. #include <stdio.h>
  2.  
  3. // Define the structure to hold the totals for each county code
  4. struct countryTotals {
  5. int totalCorkCodes;
  6. int totalDublinCodes;
  7. int totalGalwayCodes;
  8. int totalLimerickCodes;
  9. int totalTiperaryCodes;
  10. int totalWaterfordCodes;
  11. int totalInvalidCountryCodes;
  12. };
  13.  
  14. // Function prototype
  15. struct countryTotals countCountyCodes(char countyCodes[], int size);
  16.  
  17. // Function definition
  18. /**
  19.  * Function Name: countCountyCodes
  20.  *
  21.  * Function Block:
  22.  * Counts the occurrences of each valid Irish county code in the given array.
  23.  *
  24.  * @param countyCodes[] The array of characters containing county codes.
  25.  * @param size The size of the countyCodes array.
  26.  * @return A structure containing the total counts for each county code.
  27.  */
  28. struct countryTotals countCountyCodes(char countyCodes[], int size) {
  29. struct countryTotals totals = {0, 0, 0, 0, 0, 0, 0}; // Initialize all counts to 0
  30.  
  31. for (int i = 0; i < size; i++) {
  32. switch (countyCodes[i]) {
  33. case 'C': case 'c':
  34. totals.totalCorkCodes++;
  35. break;
  36. case 'D': case 'd':
  37. totals.totalDublinCodes++;
  38. break;
  39. case 'G': case 'g':
  40. totals.totalGalwayCodes++;
  41. break;
  42. case 'L': case 'l':
  43. totals.totalLimerickCodes++;
  44. break;
  45. case 'T': case 't':
  46. totals.totalTiperaryCodes++;
  47. break;
  48. case 'W': case 'w':
  49. totals.totalWaterfordCodes++;
  50. break;
  51. default:
  52. totals.totalInvalidCountryCodes++;
  53. break;
  54. }
  55. }
  56.  
  57. return totals;
  58. }
  59.  
  60. int main() {
  61. char countyCodes[] = {'C', 'd', 'G', 'L', 't', 'W', 'X', 'c', 'D', 'g', 'l', 'T', 'w', 'Y'};
  62. int size = sizeof(countyCodes) / sizeof(countyCodes[0]);
  63.  
  64. // Call the countCountyCodes function and get the result
  65. struct countryTotals result = countCountyCodes(countyCodes, size);
  66.  
  67. // Print the results
  68. printf("Cork Codes: %d\n", result.totalCorkCodes);
  69. printf("Dublin Codes: %d\n", result.totalDublinCodes);
  70. printf("Galway Codes: %d\n", result.totalGalwayCodes);
  71. printf("Limerick Codes: %d\n", result.totalLimerickCodes);
  72. printf("Tiperary Codes: %d\n", result.totalTiperaryCodes);
  73. printf("Waterford Codes: %d\n", result.totalWaterfordCodes);
  74. printf("Invalid Codes: %d\n", result.totalInvalidCountryCodes);
  75.  
  76. return 0;
  77. }
Success #stdin #stdout 0s 5268KB
stdin
Standard input is empty
stdout
Cork Codes: 2
Dublin Codes: 2
Galway Codes: 2
Limerick Codes: 2
Tiperary Codes: 2
Waterford Codes: 2
Invalid Codes: 2