Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/bag.c
Last active Jul 7, 2016

Embed
What would you like to do?
[2016-06-20] Challenge #272 [Easy] What's in the bag?
#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
You can’t perform that action at this time.