fork download
  1. // Assignment Final Question 3
  2. //
  3. // Name: <Maribel Fuentes>
  4. //
  5. // Class: C Programming, <Fall 2024>
  6. //
  7. // Date: <December 2, 2024>
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11.  
  12. int cardValue(char card) {
  13. if (card >= '2' && card <= '9') {
  14. return card - '0';
  15. } else if (card == 'T' || card == 'K' || card == 'Q' || card == 'J') {
  16. return 10;
  17. } else if (card == 'A') {
  18. return 11;
  19. } else {
  20. return -1; // Invalid card
  21. }
  22. }
  23.  
  24. int blackjackHandValue(char card1, char card2) {
  25. int value1 = cardValue(card1);
  26. int value2 = cardValue(card2);
  27.  
  28. if (value1 == -1 || value2 == -1) {
  29. printf("*** Invalid card input\n");
  30. return -1;
  31. }
  32.  
  33. int total = value1 + value2;
  34.  
  35. // Adjust for the case of two aces
  36. if (card1 == 'A' && card2 == 'A') {
  37. total = 12;
  38. }
  39.  
  40. return total;
  41. }
  42.  
  43. int main() {
  44. char card1, card2;
  45.  
  46. printf("Enter cards: ");
  47. scanf(" %c %c", &card1, &card2);
  48.  
  49. // Convert to uppercase to handle lowercase input
  50. card1 = toupper(card1);
  51. card2 = toupper(card2);
  52.  
  53. int score = blackjackHandValue(card1, card2);
  54.  
  55. if (score != -1) {
  56. printf("The score is %d\n", score);
  57. }
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 5284KB
stdin
A Q
A A
T 7
A 5
7 #
Z 4
stdout
Enter cards: The score is 21