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

stdout
Enter cards: *** Invalid card input