Last active
July 7, 2016 17:01
-
-
Save CraigRodrigues/719a7ede9915c41ba0464f203af3f6f4 to your computer and use it in GitHub Desktop.
[2016-06-20] Challenge #272 [Easy] What's in the bag?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main (void) | |
{ | |
int tilecount[] = { 9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, | |
4, 6, 4, 2, 2, 1, 2, 1, 0, 0, 0, 0, 2 }; | |
char* input = GetString(); | |
// iterate through input and decrememnt the tile count | |
for (int i = 0; i < strlen(input); i++) | |
{ | |
if ((input[i] > 64 && input[i] < 91) || input[i] == 95) // check if a valid tile char | |
tilecount[(input[i]) - 65]--; | |
// check if tilecount went below 0. If so print error. | |
if (tilecount[(input[i]) - 65] < 0) | |
{ | |
printf("Invalid input. More %c's have been taken from the bag than possible.\n", input[i]); | |
return -1; | |
} | |
} | |
// print output | |
for (int j = 12; j >= 0; j--) | |
{ | |
int printed = 0; // check if count printed out | |
for (int k = 0; k < 31; k++) | |
{ | |
if (printed == 1) | |
if (tilecount[k] == j) | |
if (((k + 65) > 64 && (k + 65) < 91) || (k + 65) == 95) | |
printf(", %c", k + 65); | |
if (printed == 0) | |
if (tilecount[k] == j) | |
{ | |
if (((k + 65) > 64 && (k + 65) < 91) || (k + 65) == 95) | |
{ | |
printf("%i: %c", j, k + 65); | |
printed = 1; | |
} | |
} | |
} | |
if (printed == 1) | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment